Example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setAlignment

List of usage examples for org.apache.poi.xssf.usermodel XSSFCellStyle setAlignment

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setAlignment.

Prototype

@Override
public void setAlignment(HorizontalAlignment align) 

Source Link

Document

Set the type of horizontal alignment for the cell

Usage

From source file:ru.jeene.zapretparser.controller.XLSXReportController.java

public void WriteReport(FullReport rep, String timestamp_csv) {
    try (FileInputStream inp = new FileInputStream(shab_name)) {
        XSSFWorkbook wb = new XSSFWorkbook(inp); // Declare XSSF WorkBook
        XSSFSheet sheet = wb.getSheet(" ");
        XSSFCellStyle cs1 = wb.createCellStyle();
        //cs1.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        cs1.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cs1.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cs1.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cs1.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cs1.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        cs1.setWrapText(true);/*from  w w w  .j a  v  a  2 s  .  c  o m*/
        cs1.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        XSSFFont f = wb.createFont();
        f.setBold(false);
        f.setFontName("Times New Roman");
        f.setFontHeightInPoints((short) 14);
        cs1.setFont(f);
        int cnt = 0;
        XSSFRow row;
        XSSFCell cell;
        for (Model_FullReport m : rep.getList()) {
            int cnt_cell = 0;

            row = sheet.getRow(t1_start - 1 + cnt);
            if (row == null) {
                row = sheet.createRow(t1_start - 1 + cnt);
            }
            //? URL
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getElement().getUrl());
            cell.setCellStyle(cs1);
            cnt_cell++;

            //
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getElement().getOrg());
            cell.setCellStyle(cs1);
            cnt_cell++;

            //? 
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getElement().getDoc());
            cell.setCellStyle(cs1);
            cnt_cell++;
            //
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getElement().getDate());
            cell.setCellStyle(cs1);
            cnt_cell++;

            //
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getResult().name());
            cell.setCellStyle(cs1);
            cnt_cell++;

            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(m.getResult().getDesc());
            cell.setCellStyle(cs1);
            cnt_cell++;

            // ()
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(m.getResult().getCode());
            cell.setCellStyle(cs1);
            cnt_cell++;
            cnt++;
        }
        // 
        row = sheet.getRow(0);
        cell = row.getCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(MAIN_ZAG_TEMPL + timestamp_csv);

        //   
        HashMap<ResponseResult, Model_NumberReport> map = rep.reportCountBytype();

        //  ?
        sheet = wb.getSheet("");

        //  
        cnt = 0;
        for (Map.Entry<ResponseResult, Model_NumberReport> entry : map.entrySet()) {
            ResponseResult key = entry.getKey();
            Model_NumberReport value = entry.getValue();

            int cnt_cell = 0;
            row = sheet.getRow(t0_start - 1 + cnt);
            if (row == null) {
                row = sheet.createRow(t0_start - 1 + cnt);
            }
            /*//
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_STRING);
             cell.setCellValue(key.name());
             cell.setCellStyle(cs1);
             cnt_cell++;*/

            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(key.getDesc());
            cell.setCellStyle(cs1);
            cnt_cell++;

            /*// ()
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
             cell.setCellValue(key.getCode());
             cell.setCellStyle(cs1);
             cnt_cell++;*/
            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(value.getNumber());
            cell.setCellStyle(cs1);
            cnt_cell++;
            // ()
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(FormatUtils.FormatDoubleD(value.getPercent()));
            cell.setCellStyle(cs1);
            cnt_cell++;
            cnt++;
        }

        // 
        row = sheet.getRow(0);
        cell = row.getCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(MAIN_ZAG_TEMPL + timestamp_csv);

        //    HTTP
        map = rep.reportCountBytypeHTTP();
        //  ?
        sheet = wb.getSheet(" HTTP");

        //  
        cnt = 0;
        for (Map.Entry<ResponseResult, Model_NumberReport> entry : map.entrySet()) {
            ResponseResult key = entry.getKey();
            Model_NumberReport value = entry.getValue();

            int cnt_cell = 0;
            row = sheet.getRow(t0_start - 1 + cnt);
            if (row == null) {
                row = sheet.createRow(t0_start - 1 + cnt);
            }
            /*//
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_STRING);
             cell.setCellValue(key.name());
             cell.setCellStyle(cs1);
             cnt_cell++;*/

            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(key.getDesc());
            cell.setCellStyle(cs1);
            cnt_cell++;

            /*// ()
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
             cell.setCellValue(key.getCode());
             cell.setCellStyle(cs1);
             cnt_cell++;*/
            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(value.getNumber());
            cell.setCellStyle(cs1);
            cnt_cell++;
            // ()
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(FormatUtils.FormatDoubleD(value.getPercent()));
            cell.setCellStyle(cs1);
            cnt_cell++;
            cnt++;
        }

        // 
        row = sheet.getRow(0);
        cell = row.getCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(MAIN_ZAG_TEMPL + timestamp_csv);

        //    HTTPS
        map = rep.reportCountBytypeHTTPS();
        //  ?
        sheet = wb.getSheet(" HTTPS");

        //  
        cnt = 0;
        for (Map.Entry<ResponseResult, Model_NumberReport> entry : map.entrySet()) {
            ResponseResult key = entry.getKey();
            Model_NumberReport value = entry.getValue();

            int cnt_cell = 0;
            row = sheet.getRow(t0_start - 1 + cnt);
            if (row == null) {
                row = sheet.createRow(t0_start - 1 + cnt);
            }
            /*//
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_STRING);
             cell.setCellValue(key.name());
             cell.setCellStyle(cs1);
             cnt_cell++;*/

            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(key.getDesc());
            cell.setCellStyle(cs1);
            cnt_cell++;

            /*// ()
             cell = row.getCell(cnt_cell);
             if (cell == null) {
             cell = row.createCell(cnt_cell);
             }
             cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
             cell.setCellValue(key.getCode());
             cell.setCellStyle(cs1);
             cnt_cell++;*/
            // (?)
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(value.getNumber());
            cell.setCellStyle(cs1);
            cnt_cell++;
            // ()
            cell = row.getCell(cnt_cell);
            if (cell == null) {
                cell = row.createCell(cnt_cell);
            }
            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(FormatUtils.FormatDoubleD(value.getPercent()));
            cell.setCellStyle(cs1);
            cnt_cell++;
            cnt++;
        }

        // 
        row = sheet.getRow(0);
        cell = row.getCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(MAIN_ZAG_TEMPL + timestamp_csv);

        //? 
        String tmp_out = StringUtils.replaceAll(report_name, "!dt!",
                DateUtils.DateToString(new Date(System.currentTimeMillis()), "ddMMyyyy_Hms"));
        try (FileOutputStream out = new FileOutputStream(tmp_out)) {
            wb.write(out);
            logger.info("Report file " + tmp_out + " created");
        }

    } catch (Exception ex) {
        logger.error(ex);
    }
}

From source file:Servelt.ExcelWriter.java

private void setMainCell(XSSFCell cell, String name) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

    XSSFFont font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);//from w w  w . j a v a 2s .c o m

    cell.setCellValue(name);
    cell.setCellStyle(style);
}

From source file:Servelt.ExcelWriter.java

private void setAttrCell(XSSFCell cell, String name) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.YELLOW.index);
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

    XSSFFont font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);//from   w  w w  .  j a va2  s  .c  o m

    cell.setCellValue(name);
    cell.setCellStyle(style);
}

From source file:Servelt.ExcelWriter.java

private void setDesCell(XSSFCell cell, String name) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.GREEN.index);
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

    XSSFFont font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);/*w  w  w  .ja  va 2 s . c  o m*/

    cell.setCellValue(name);
    cell.setCellStyle(style);
}

From source file:tools.IOHelper.java

public static void plate1ResultSheet(ANAPlate plate, File outputFolder) throws IOException {

    File outputFile = new File(outputFolder, plate.getPlateId() + "_" + TIME + ".xlsx");
    Workbook excelFile = null;/*from   w w w .j  a  va  2  s.c o m*/
    //        if (outputFile.exists()) {
    //            try {
    //                excelFile = WorkbookFactory.create(outputFile);
    //            } catch (EncryptedDocumentException ex) {
    //                System.out.println("file with assigned name already exists but is encrypted...");
    //                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    //            } catch (InvalidFormatException ex) {
    //                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    //            }
    //        } else {
    //            outputFile.createNewFile();
    //            excelFile = new XSSFWorkbook();
    //        }
    if (outputFile.exists()) {
        outputFile.delete();
    }
    outputFile.createNewFile();
    excelFile = new XSSFWorkbook();
    if (excelFile == null) {
        throw new RuntimeException("fail to create the xlsx file");
    }
    int rowCount = plate.getSampleNumber(); //not including the 2 control samples + "_"+time
    String sheetName = plate.getPlateId();
    //create a working sheet 
    Sheet sheet = excelFile.createSheet(sheetName);
    //starting row & col
    int rowIndex = 0;
    int colIndex = 0;
    int totalCol = 0;
    int pos = 0, neg = 0, all = 0;
    XSSFFont fontTitle = (XSSFFont) excelFile.createFont();
    fontTitle.setFontHeightInPoints((short) 10);
    fontTitle.setFontName("Arial");
    fontTitle.setColor(IndexedColors.GREEN.getIndex());
    fontTitle.setBold(true);
    fontTitle.setItalic(false);
    XSSFCellStyle styleTitle = (XSSFCellStyle) excelFile.createCellStyle();
    styleTitle.setAlignment(CellStyle.ALIGN_CENTER);
    styleTitle.setFont(fontTitle);
    Cell cell0 = sheet.createRow(rowIndex++).createCell(0);
    cell0.setCellValue(plate.getPlateId() + " Summary"); //title
    cell0.setCellStyle(styleTitle);

    //        //optional set Cell Style
    //        CellStyle styleTitle = null;
    //        CellStyle style = null;
    Row row = sheet.createRow(rowIndex++); //names
    Cell column = row.createCell(colIndex++);
    column.setCellValue("Sample ID");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Chip Location");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Result");
    totalCol++;

    //        column = row.createCell(colIndex++);
    //        column.setCellValue("Positivity 0.3P");
    //        totalCol++;
    //        column = row.createCell(colIndex++);
    //        column.setCellValue("Positivity0.275P+0.5N");
    //        totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Signal");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Comments");
    totalCol++;

    for (ANATestResult result : plate.getTestResultList()) {
        row = sheet.createRow(rowIndex++); //data
        colIndex = 0;
        column = row.createCell(colIndex++);
        column.setCellValue(result.getJulien_barcode());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getPillarPosition());
        column = row.createCell(colIndex++);
        if (result.getPositivity() == null) {
            column.setCellValue("Null Result");
        } else {
            if (ANA_Result.POSITIVE.equals(result.getPositivity())) {
                pos++;
            } else if (ANA_Result.NEGATIVE.equals(result.getPositivity())) {
                neg++;
            }
            all++;
            column.setCellValue(result.getPositivity().name());
        } //make sure all fis has not-null pn result

        //            column = row.createCell(colIndex++);
        //            if (result.positivity30 == null) {
        //                column.setCellValue("Null Result");
        //            } else {
        //                if (ANA_Result.POSITIVE.equals(result.positivity30)) {
        ////                    pos++;
        //                } else if (ANA_Result.NEGATIVE.equals(result.positivity30)) {
        ////                    neg++;
        //                }
        ////                all++;
        //                column.setCellValue(result.positivity30.name());
        //            }  //make sure all fis has not-null pn result
        //            column = row.createCell(colIndex++);
        //            if (result.positivityCombined == null) {
        //                column.setCellValue("Null Result");
        //            } else {
        //                if (ANA_Result.POSITIVE.equals(result.positivityCombined)) {
        ////                    pos++;
        //                } else if (ANA_Result.NEGATIVE.equals(result.positivityCombined)) {
        ////                    neg++;
        //                }
        ////                all++;
        //                column.setCellValue(result.positivityCombined.name());
        //            }  //make sure all fis has not-null pn result

        column = row.createCell(colIndex++);
        column.setCellValue(result.getFirstPlateSignal());
        //            if(result.getFirstPlateSignal()<0){
        //                column.setCellValue("ROI exception: unable to get signal for this sample");
        //            }else{
        //                column.setCellValue(result.getFirstPlateSignal());
        //            }
        column = row.createCell(colIndex++); //warning msg concat mthd; merge plateErr to sampErr
        column.setCellValue(result.concatWarningMsgs());
    }
    if (rowIndex - rowCount == 2) {
        for (int i = 0; i < totalCol; i++) {
            sheet.autoSizeColumn(i);
        }
    } else {
        System.out
                .println((rowIndex - 2) + " records are writen into file while " + rowCount + " are expected");
    }
    row = sheet.createRow(rowIndex++); //total
    colIndex = 0;
    column = row.createCell(colIndex++);
    column.setCellValue("all samples");
    column = row.createCell(colIndex++);
    column.setCellValue(all);
    column = row.createCell(colIndex++);
    column.setCellValue("positive samples");
    column = row.createCell(colIndex++);
    column.setCellValue(pos);
    column = row.createCell(colIndex++);
    column.setCellValue("negative samples");
    column = row.createCell(colIndex++);
    column.setCellValue(neg);

    column = row.createCell(colIndex++);
    column = row.createCell(colIndex++);
    column = row.createCell(colIndex++);
    column.setCellValue("PosCtrl");
    column = row.createCell(colIndex++);
    column.setCellValue(plate.getPosCtrl().getFirstPlateSignal());
    column = row.createCell(colIndex++);
    column.setCellValue("NegCtrl");
    column = row.createCell(colIndex++);
    column.setCellValue(plate.getNegCtrlSignal());

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile.getAbsolutePath());//,true
        excelFile.write(fos);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:tools.IOHelper.java

public static void plate2ResultSheet(ANAPlate plate, File outputFolder) throws IOException {

    File outputFile = new File(outputFolder, plate.getPlateId() + "_" + TIME + ".xlsx");
    Workbook excelFile = null;/*w w  w  .  j a  v  a2  s.  com*/
    if (outputFile.exists()) {
        outputFile.delete();
    }
    outputFile.createNewFile();
    excelFile = new XSSFWorkbook();
    if (excelFile == null) {
        throw new RuntimeException("fail to create the xlsx file");
    }
    int rowCount = plate.getSampleNumber(); //not including the 2 control samples + "_"+time
    String sheetName = plate.getPlateId();
    //create a working sheet 
    Sheet sheet = excelFile.createSheet(sheetName);
    //starting row & col
    int rowIndex = 0;
    int colIndex = 0;
    int totalCol = 0;
    int pos = 0, neg = 0, all = 0;
    XSSFFont fontTitle = (XSSFFont) excelFile.createFont();
    fontTitle.setFontHeightInPoints((short) 10);
    fontTitle.setFontName("Arial");
    fontTitle.setColor(IndexedColors.GREEN.getIndex());
    fontTitle.setBold(true);
    fontTitle.setItalic(false);
    XSSFCellStyle styleTitle = (XSSFCellStyle) excelFile.createCellStyle();
    styleTitle.setAlignment(CellStyle.ALIGN_CENTER);
    styleTitle.setFont(fontTitle);
    Cell cell0 = sheet.createRow(rowIndex++).createCell(0);
    cell0.setCellValue(plate.getPlateId() + " Summary"); //title
    cell0.setCellStyle(styleTitle);

    //        //optional set Cell Style
    //        CellStyle styleTitle = null;
    //        CellStyle style = null;

    //            add column:Sample,Chip Location,Signal,Positivity,sample titer,plate titer, pattern, No of Cells, enableWatershed,comment
    Row row = sheet.createRow(rowIndex++); //names
    Cell column = row.createCell(colIndex++);
    column.setCellValue("Sample ID");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Chip Location");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Signal");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Positivity");
    totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Sample Titer");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Plate Titer");
    totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Pattern");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Number of Cells");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Comments");
    totalCol++;

    for (ANATestResult result : plate.getTestResultList()) {
        row = sheet.createRow(rowIndex++); //data
        colIndex = 0;
        column = row.createCell(colIndex++);
        column.setCellValue(result.getJulien_barcode());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getPillarPosition());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getSecondPlateSignal());
        column = row.createCell(colIndex++);
        if (result.getPositivity() == null) {
            column.setCellValue("Null Result");
        } else {
            if (ANA_Result.POSITIVE.equals(result.getPositivity())) {
                pos++;
            } else if (ANA_Result.NEGATIVE.equals(result.getPositivity())) {
                neg++;
            }
            all++;
            column.setCellValue(result.getPositivity().name());
        } //make sure all fis has not-null pn result

        column = row.createCell(colIndex++);
        if (result.getTiter() != null) {
            column.setCellValue(result.getTiter().name());
        }
        column = row.createCell(colIndex++);
        if (plate.getPosCtrl().getTiter() != null) {
            column.setCellValue(plate.getPosCtrl().getTiter().name());
        }
        //pattern, No of Cells, enableWatershed,comment
        column = row.createCell(colIndex++);
        if (result.getPattern() != null) {
            column.setCellValue(result.getPattern().name());
        }
        column = row.createCell(colIndex++);
        column.setCellValue(result.cellCount());
        column = row.createCell(colIndex++); //warning msg concat mthd; merge plateErr to sampErr
        column.setCellValue(result.concatWarningMsgs());
    }
    if (rowIndex - rowCount == 2) {
        for (int i = 0; i < totalCol; i++) {
            sheet.autoSizeColumn(i);
        }
    } else {
        System.out
                .println((rowIndex - 2) + " records are writen into file while " + rowCount + " are expected");
    }
    row = sheet.createRow(rowIndex++); //total
    colIndex = 0;
    column = row.createCell(colIndex++);
    column.setCellValue("all samples");
    column = row.createCell(colIndex++);
    column.setCellValue(all);
    column = row.createCell(colIndex++);
    column.setCellValue("positive samples");
    column = row.createCell(colIndex++);
    column.setCellValue(pos);
    column = row.createCell(colIndex++);
    column.setCellValue("negative samples");
    column = row.createCell(colIndex++);
    column.setCellValue(neg);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile.getAbsolutePath());//,true
        excelFile.write(fos);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:tr.org.liderahenk.liderconsole.core.utils.SWTResourceManager.java

License:Open Source License

private static XSSFWorkbook createWorkbookFromTable(TableViewer tableViewer, String sheetName) {

    // Create workbook & sheet
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet(sheetName == null ? "Sheet1" : sheetName);

    // Shade the background of the header row
    XSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
    headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    headerStyle.setAlignment(HorizontalAlignment.CENTER);

    // Add header row
    Table table = tableViewer.getTable();
    TableColumn[] columns = table.getColumns();
    int rowIndex = 0;
    int cellIndex = 0;
    XSSFRow header = sheet.createRow((short) rowIndex++);
    for (TableColumn column : columns) {
        XSSFCell cell = header.createCell(cellIndex++);
        cell.setCellValue(column.getText());
        cell.setCellStyle(headerStyle);//www . ja v a 2 s. com
    }

    // Add data rows
    TableItem[] items = tableViewer.getTable().getItems();
    for (TableItem item : items) {
        // create a new row
        XSSFRow row = sheet.createRow((short) rowIndex++);
        cellIndex = 0;

        for (int i = 0; i < columns.length; i++) {
            // Create a new cell
            XSSFCell cell = row.createCell(cellIndex++);
            String text = item.getText(i);

            // Set the horizontal alignment (default to RIGHT)
            XSSFCellStyle cellStyle = wb.createCellStyle();
            if (LiderCoreUtils.isInteger(text)) {
                cellStyle.setAlignment(HorizontalAlignment.RIGHT);
            } else if (LiderCoreUtils.isValidDate(text,
                    ConfigProvider.getInstance().get(LiderConstants.CONFIG.DATE_FORMAT))) {
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            } else {
                cellStyle.setAlignment(HorizontalAlignment.LEFT);
            }
            cell.setCellStyle(cellStyle);

            // Set the cell's value
            cell.setCellValue(text);
        }
    }

    // Auto-fit the columns
    for (int i = 0; i < columns.length; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return wb;
}

From source file:uk.co.certait.htmlexporter.writer.excel.ExcelStyleGenerator.java

License:Apache License

protected void applyHorizontalAlignment(Style style, XSSFCellStyle cellStyle) {
    if (style.isHorizontallyAlignedLeft()) {
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
    } else if (style.isHorizontallyAlignedRight()) {
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
    } else if (style.isHorizontallyAlignedCenter()) {
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
    }//w ww  .j av  a2  s .  c o  m
}

From source file:vd10_workbook.AbilityManagement.java

public void createWorkSheet(XSSFWorkbook workbook) {
    XSSFSheet sheet = workbook.createSheet("kha_nang");
    int startRow = 0;
    XSSFRow row = sheet.createRow((short) startRow);

    //== THE TITLE ==//
    //SET HEIGHT OF ROW 2 (in excel)
    row.setHeight((short) 500);
    XSSFCell cell = (XSSFCell) row.createCell((short) 0);
    cell.setCellValue("Kh nng lm vic");

    //MEARGING CELLS 
    //this statement for merging cells
    CellRangeAddress cellRangeAddress = new CellRangeAddress(startRow, //first row (0-based)
            startRow, //last row (0-based)
            0, //first column (0-based)
            2 //last column (0-based)
    );/*from www .ja  v a  2s .co m*/
    sheet.addMergedRegion(cellRangeAddress);

    // Center Align Cell Contents 
    XSSFCellStyle align = workbook.createCellStyle();
    align.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    align.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cell.setCellStyle(align);

    //set border
    this.setRegionBoder(cellRangeAddress, workbook, sheet);

    //==THE LABELS ==//
    //STT
    row = sheet.createRow((short) startRow + 1);
    row.setHeight((short) 400);
    cell = (XSSFCell) row.createCell((short) 0);

    cell.setCellValue("STT");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //ID_NHAN_VIEN
    sheet.setColumnWidth(1, 5000);
    cell = (XSSFCell) row.createCell((short) 1);
    cell.setCellValue("ID Nhn vin");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //ID_NGOAI_NGU
    sheet.setColumnWidth(2, 5000);
    cell = (XSSFCell) row.createCell((short) 2);
    cell.setCellValue("ID Ngoi ng");
    this.setThickBorder(cell, workbook);
    this.setBackGroundColor(cell, workbook);

    //fill out the rows
    for (int i = 0; i < this.list.size(); i++) {
        row = sheet.createRow((short) startRow + 2 + i);
        cell = (XSSFCell) row.createCell((short) 0);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(i + 1);

        cell = (XSSFCell) row.createCell((short) 1);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getEmployeeID());

        cell = (XSSFCell) row.createCell((short) 2);
        this.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getLanguageID());

    }
}

From source file:vd10_workbook.AssignedVoteManagement.java

public void createWorkSheet(XSSFWorkbook workbook) {
    XSSFSheet sheet = workbook.createSheet("phieu_phan_cong");
    int startRow = 0;
    XSSFRow row = sheet.createRow((short) startRow);

    //== THE TITLE ==//
    //SET HEIGHT OF ROW 2 (in excel)
    row.setHeight((short) 500);
    XSSFCell cell = (XSSFCell) row.createCell((short) 0);
    cell.setCellValue("Phiu phn cng");

    //MEARGING CELLS 
    //this statement for merging cells
    CellRangeAddress cellRangeAddress = new CellRangeAddress(startRow, //first row (0-based)
            startRow, //last row (0-based)
            0, //first column (0-based)
            4 //last column (0-based)
    );//from ww  w  . j  av  a 2 s  . co  m
    sheet.addMergedRegion(cellRangeAddress);
    // Center Align Cell Contents 
    XSSFCellStyle align = workbook.createCellStyle();
    align.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    align.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cell.setCellStyle(align);

    //set border
    AbilityManagement.setRegionBoder(cellRangeAddress, workbook, sheet);

    //==THE LABELS ==//
    //STT
    row = sheet.createRow((short) startRow + 1);
    row.setHeight((short) 400);
    cell = (XSSFCell) row.createCell((short) 0);

    cell.setCellValue("STT");
    AbilityManagement.setThickBorder(cell, workbook);

    //Ngy bt u
    sheet.setColumnWidth(1, 5000);
    cell = (XSSFCell) row.createCell((short) 1);
    cell.setCellValue("Ngy bt u");
    AbilityManagement.setThickBorder(cell, workbook);

    //S ngy
    sheet.setColumnWidth(2, 5000);
    cell = (XSSFCell) row.createCell((short) 2);
    cell.setCellValue("S ngy");
    AbilityManagement.setThickBorder(cell, workbook);

    //ID nhn vin
    sheet.setColumnWidth(3, 5000);
    cell = (XSSFCell) row.createCell((short) 3);
    cell.setCellValue("ID nhn vin");
    AbilityManagement.setThickBorder(cell, workbook);

    //ID loi cng vic
    sheet.setColumnWidth(4, 5000);
    cell = (XSSFCell) row.createCell((short) 4);
    cell.setCellValue("ID loi cng vic");
    AbilityManagement.setThickBorder(cell, workbook);

    //fill out the rows
    for (int i = 0; i < this.list.size(); i++) {
        row = sheet.createRow((short) startRow + 2 + i);
        cell = (XSSFCell) row.createCell((short) 0);
        AbilityManagement.setThinBorder(cell, workbook);
        cell.setCellValue(i + 1);

        cell = (XSSFCell) row.createCell((short) 1);
        AbilityManagement.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getStartDate().toString());

        cell = (XSSFCell) row.createCell((short) 2);
        AbilityManagement.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getNumberOfdate());

        cell = (XSSFCell) row.createCell((short) 3);
        AbilityManagement.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getEmployeeID());

        cell = (XSSFCell) row.createCell((short) 4);
        AbilityManagement.setThinBorder(cell, workbook);
        cell.setCellValue(this.list.get(i).getWorkTypeID());
    }

    //== calculate sum of days ==//
    row = sheet.createRow((short) startRow + 2 + this.list.size());
    cell = (XSSFCell) row.createCell((short) 0);
    cell.setCellValue("Tng");
    //merge cells at column 1 & 2
    cellRangeAddress = new CellRangeAddress(startRow + 2 + this.list.size(), //first row (0-based)
            startRow + 2 + this.list.size(), //last row (0-based)
            0, //first column (0-based)
            1 //last column (0-based)
    );
    sheet.addMergedRegion(cellRangeAddress);
    //set border
    RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
    RegionUtil.setBorderRight(XSSFCellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
    RegionUtil.setBorderTop(XSSFCellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
    RegionUtil.setBorderLeft(XSSFCellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
    cell = row.createCell((short) 2);
    cell.setCellFormula("SUM(C" + (startRow + 1 + 2) + ":C" + (startRow + 2 + this.list.size()) + ")");
    AbilityManagement.setThickBorder(cell, workbook);
}