Example usage for org.apache.poi.ss.usermodel CellStyle setShrinkToFit

List of usage examples for org.apache.poi.ss.usermodel CellStyle setShrinkToFit

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CellStyle setShrinkToFit.

Prototype

void setShrinkToFit(boolean shrinkToFit);

Source Link

Document

Controls if the Cell should be auto-sized to shrink to fit if the text is too long

Usage

From source file:com.crimelab.service.ChemistryServiceImpl.java

@Override
public Workbook createMonthlyReport(HttpSession session, String month) {
    Workbook wb = null;/* www  . j  a  v  a  2s  .  co  m*/

    try {
        InputStream inp = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/DrugMonthlyReport.xls");
        wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        CellStyle cs1 = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();
        CellStyle bl = wb.createCellStyle();
        CellStyle br = wb.createCellStyle();
        CellStyle bt = wb.createCellStyle();
        CellStyle bb = wb.createCellStyle();
        CellStyle stf = wb.createCellStyle();
        cs1.setWrapText(true);
        cs2.setAlignment(ALIGN_CENTER);
        bt.setBorderTop(BORDER_THIN);
        bb.setBorderBottom(BORDER_THIN);
        bl.setBorderLeft(BORDER_THIN);
        br.setBorderRight(BORDER_THIN);
        stf.setShrinkToFit(true);

        Row intro1 = sheet.createRow(7);
        Cell in1 = intro1.createCell(0);//.setCellValue("DRUG INVETORY COVERED PERIOD JANUARY-DECEMBER CY-" +month.split("-")[0]);
        in1.setCellValue("DRUG INVENTORY COVERED PERIOD JANUARY-DECEMBER CY-" + month.split("-")[0]);
        in1.setCellStyle(cs1);
        in1.setCellStyle(cs2);

        Row intro2 = sheet.createRow(9);
        Cell in2 = intro2.createCell(0);//.setCellValue("SUMMARY OF SEIZED/SURRENDERED/RECOVERED OF DRUG EVIDENCES FROM NEGATION OPERATIONS FROM LAW ENFORCEMENTS, PHARMACEUTICAL COMPANIES AND SIMILAR ESTABLISHMENTS FOR THE MONTH OF "+month);
        in2.setCellValue(
                "SUMMARY OF SEIZED/SURRENDERED/RECOVERED OF DRUG EVIDENCES FROM NEGATION OPERATIONS FROM LAW ENFORCEMENTS, PHARMACEUTICAL COMPANIES AND SIMILAR ESTABLISHMENTS FOR THE MONTH OF "
                        + month);
        in2.setCellStyle(cs1);
        in2.setCellStyle(cs2);
        in2.setCellStyle(stf);

        int ctr = 12; //initial
        Row row = sheet.createRow(ctr);
        month = month.split("-")[1];

        //System.out.println("GAC " + chemistryDAO.getAllChemistry(month).isEmpty());
        for (Chemistry chemistry : chemistryDAO.getAllChemistry(month)) {
            //System.out.println("Test " + chemistry.getTimeDateReceived());
            Cell cell0 = row.createCell(0);
            cell0.setCellValue(chemistry.getTimeDateReceived());
            cell0.setCellStyle(bt);
            cell0.setCellStyle(bb);
            cell0.setCellStyle(bl);
            cell0.setCellStyle(br);

            Cell cell1 = row.createCell(1);//.setCellValue(chemistry.getReportNo());
            cell1.setCellValue(chemistry.getReportNo());
            cell1.setCellStyle(bt);
            cell1.setCellStyle(bb);
            cell1.setCellStyle(bl);
            cell1.setCellStyle(br);

            Cell cell2 = row.createCell(2);//.setCellValue(chemistry.getRequestingParty());
            cell2.setCellValue(chemistry.getRequestingParty());
            cell2.setCellStyle(bt);
            cell2.setCellStyle(bb);
            cell2.setCellStyle(bl);
            cell2.setCellStyle(br);

            Cell cell3 = row.createCell(3);//.setCellValue(chemistry.getDescriptionOfEvidence());
            cell3.setCellValue(chemistry.getDescriptionOfEvidence());
            cell3.setCellStyle(bt);
            cell3.setCellStyle(bb);
            cell3.setCellStyle(bl);
            cell3.setCellStyle(br);

            Cell cell4 = row.createCell(4);//.setCellValue(chemistry.getSpecimenWeight());
            cell4.setCellValue(chemistry.getSpecimenWeight());
            cell4.setCellStyle(bt);
            cell4.setCellStyle(bb);
            cell4.setCellStyle(bl);
            cell4.setCellStyle(br);

            Cell cell5 = row.createCell(5);//.setCellValue(chemistry.getCustody());
            cell5.setCellValue(chemistry.getCustody());
            cell5.setCellStyle(bt);
            cell5.setCellStyle(bb);
            cell5.setCellStyle(bl);
            cell5.setCellStyle(br);

            Cell cell6 = row.createCell(6);//.setCellValue(chemistry.getSuspects());
            cell6.setCellValue(chemistry.getSuspects());
            cell6.setCellStyle(bt);
            cell6.setCellStyle(bb);
            cell6.setCellStyle(bl);
            cell6.setCellStyle(br);

            Cell cell7 = row.createCell(7);//.setCellValue(chemistry.getTypeOfOperation());
            cell7.setCellValue(chemistry.getTypeOfOperation());
            cell7.setCellStyle(bt);
            cell7.setCellStyle(bb);
            cell7.setCellStyle(bl);
            cell7.setCellStyle(br);

            Cell cell8 = row.createCell(8);//.setCellValue(chemistry.getPlaceOfOperation());
            cell8.setCellValue(chemistry.getPlaceOfOperation());
            cell8.setCellStyle(bt);
            cell8.setCellStyle(bb);
            cell8.setCellStyle(bl);
            cell8.setCellStyle(br);

            row = sheet.createRow(ctr += 1);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return wb;
}