Example usage for org.apache.poi.ss.usermodel Workbook createCellStyle

List of usage examples for org.apache.poi.ss.usermodel Workbook createCellStyle

Introduction

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

Prototype

CellStyle createCellStyle();

Source Link

Document

Create a new Cell style and add it to the workbook's style table

Usage

From source file:ch.bfh.lca._15h.library.export.ExportToExcel.java

/***
 * Generate an Excel file based on a list of results.
 * Use the name of the fields described in the GenericResultRow to label the columns.
 * @param language Language of the label in the Excel file
 * @param sheetTitle Title of the sheet in the Excel file
 * @param tableTitle Title of the table in the Excel file
 * @param rows Arrays of rows to include in the listing
 * @param excelFilePath Path of the outputed file
 * @throws FileNotFoundException/*from   w  ww . j a va2s  . c  om*/
 * @throws IOException 
 */
public static void exportToExcel(Translation.TRANSLATION_LANGUAGE language, String sheetTitle,
        String tableTitle, GenericResultRow[] rows, String excelFilePath)
        throws FileNotFoundException, IOException {
    //Workbook wb = new HSSFWorkbook(); //xls
    Workbook wb = new SXSSFWorkbook(); //xlsx
    //create new sheet
    Sheet sheet1 = wb.createSheet(sheetTitle);
    Row row;
    Cell cell;
    String translation;

    int rowIndex = 0;

    //add title
    row = sheet1.createRow((short) rowIndex);
    cell = row.createCell(0);
    cell.setCellValue(tableTitle);
    CellStyle style = wb.createCellStyle();
    Font font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    //font.setFontName("Courier New");
    style.setFont(font);
    cell.setCellStyle(style);

    //add rows
    for (int i = 0; i < rows.length; i++) {
        //if first line, write col names
        if (i == 0) {
            row = sheet1.createRow((short) rowIndex + 2);

            for (int j = 0; j < rows[i].getColNames().length; j++) {
                cell = row.createCell(j);

                //look for translation
                translation = Translation.getForKey(language, rows[i].getColNames()[j]);
                if (translation == null) {
                    translation = rows[i].getColNames()[j]; //if doesn't found a translation for the column take name of col
                }
                cell.setCellValue(translation);
            }
        }

        row = sheet1.createRow((short) (rowIndex + i + 3));

        for (int j = 0; j < rows[i].getColNames().length; j++) {
            cell = row.createCell(j);
            cell.setCellValue(rows[i].getValueAt(j).toString());
        }
    }

    //write to the file
    FileOutputStream fileOut = new FileOutputStream(excelFilePath);
    wb.write(fileOut);
    fileOut.close();

    wb.close();
}

From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java

License:Apache License

/**
 *  ?/*  w w w.j  a v  a2 s.c  o m*/
 */
public int createTitle2Row(ExportParams entity, Sheet sheet, Workbook workbook, int fieldWidth) {

    Row row = sheet.createRow(0);
    row.setHeight(entity.getTitleHeight());
    createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()),
            null);
    for (int i = 1; i <= fieldWidth; i++) {
        createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
    }
    PoiMergeCellUtil.addMergedRegion(sheet, 0, 0, 0, fieldWidth);
    if (entity.getSecondTitle() != null) {
        row = sheet.createRow(1);
        row.setHeight(entity.getSecondTitleHeight());
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.RIGHT);
        createStringCell(row, 0, entity.getSecondTitle(), style, null);
        for (int i = 1; i <= fieldWidth; i++) {
            createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
        }
        PoiMergeCellUtil.addMergedRegion(sheet, 1, 1, 0, fieldWidth);
        return 2;
    }
    return 1;
}

From source file:cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerBorderImpl.java

License:Apache License

@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setBorderLeft(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setDataFormat(STRING_FORMAT);/* ww w .  j a  v  a  2 s  .co m*/
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}

From source file:cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerColorImpl.java

License:Apache License

@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setBorderLeft(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    style.setFillForegroundColor((short) 41);
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setDataFormat(STRING_FORMAT);//w  w  w.  jav a 2  s  . c o m
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}

From source file:cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl.java

License:Apache License

@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setDataFormat(STRING_FORMAT);/*  w w  w  . j a  va 2s. com*/
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}

From source file:cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl.java

License:Apache License

@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setDataFormat(STRING_FORMAT);//  www .  ja va  2 s.c o m
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}

From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java

License:Apache License

private void createErrorCellStyle(Workbook workbook) {
    errorCellStyle = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setColor(Font.COLOR_RED);
    errorCellStyle.setFont(font);/*w  ww  .  ja v  a  2 s  .c o m*/
}

From source file:cn.bzvs.excel.export.ExcelExportServer.java

License:Apache License

/**
 *  ?//from ww  w .  j a v  a2s. c  o m
 * 
 * @param entity
 * @param sheet
 * @param workbook
 * @param feildWidth
 */
public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) {
    Row row = sheet.createRow(0);
    row.setHeight(entity.getTitleHeight());
    createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()),
            null);
    for (int i = 1; i <= feildWidth; i++) {
        createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
    }
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth));
    if (entity.getSecondTitle() != null) {
        row = sheet.createRow(1);
        row.setHeight(entity.getSecondTitleHeight());
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        createStringCell(row, 0, entity.getSecondTitle(), style, null);
        for (int i = 1; i <= feildWidth; i++) {
            createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
        }
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth));
        return 2;
    }
    return 1;
}

From source file:cn.bzvs.excel.export.styler.ExcelExportStylerBorderImpl.java

License:Apache License

@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setBorderLeft((short) 1); // 
    style.setBorderRight((short) 1); // ?
    style.setBorderBottom((short) 1);
    style.setBorderTop((short) 1);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setDataFormat(STRING_FORMAT);/*from   w  w  w.j a v a2  s .  c o  m*/
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}

From source file:cn.bzvs.excel.export.styler.ExcelExportStylerColorImpl.java

License:Apache License

@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setBorderLeft((short) 1); // 
    style.setBorderRight((short) 1); // ?
    style.setBorderBottom((short) 1);
    style.setBorderTop((short) 1);
    style.setFillForegroundColor((short) 41); // 
    style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setDataFormat(STRING_FORMAT);/*from   w w w . jav a  2s.com*/
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}