JSF -Set global font type for the excel sheet in J

2019-06-13 01:32发布

I have an option to export the datatable to an excel sheet.
I am tryin to set the font type as "Calibri" for all the cell in the sheet.
But the below code is assigning only the font type for the header not for the rest of the cells
How can I able to set the font type globally for all the cell in the sheet ?

  public void exportToXLS(Object document) {

        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        wb.setSheetName(0, "report_lists"); // set sheet name
        sheet.shiftRows(0, sheet.getLastRowNum(), 4); // shifting the rows to
        HSSFRow header = sheet.getRow(4);
        header.getCell(1).setCellValue("Test Group");
        header.getCell(2).setCellValue("Category");

        HSSFRow firstrow = sheet.getRow(0);
        firstrow.createCell(0).setCellValue("Actuals");

        SimpleDateFormat sdf = new SimpleDateFormat(
                "EEE MMM d HH:mm:ss 'CDT'  yyyy ");
        Date date = new Date();
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-5"));
        String reportDate = sdf.format(date);
        HSSFRow thirdrow = sheet.getRow(3);
        thirdrow.createCell(0).setCellValue(reportDate);

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont fontHeader = (HSSFFont) wb.createFont();
        fontHeader.setFontName("Calibri");
        cellStyle.setFont(fontHeader);
        System.out.println(" header.getPhysicalNumberOfCells();::::"
                + header.getPhysicalNumberOfCells());

        //only for changing font for header
        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);
            cell.setCellStyle(cellStyle);
            // sheet.setDefaultColumnStyle(i, cellStyle);

        }
        System.out.println("sheet.getLastRowNum():::" + sheet.getLastRowNum());
        for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
            HSSFRow row = sheet.getRow(j);
            if (row != null) {
                System.out.println(" j>>>" + j);
                // you can add sysout or debug here to check if all row passed
                // successfully
                for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                    HSSFCell cell = row.getCell(i);
                    if (cell != null) {
                        System.out.println(" i++" + i);
                        // you can add sysout or debug here to check if all cell
                        // passed successfully
                        HSSFCell celll = header.getCell(i);
                        celll.setCellStyle(cellStyle);
                    }
                }
            }
        }


    }


Here is the screen shot of the excel:
enter image description here

1条回答
ら.Afraid
2楼-- · 2019-06-13 01:59

edit your code became like this :

public void exportToXLS(Object document) {

    HSSFWorkbook wb = (HSSFWorkbook) document;
    HSSFSheet sheet = wb.getSheetAt(0);
    wb.setSheetName(0, "report_lists"); // set sheet name
    sheet.shiftRows(0, sheet.getLastRowNum(), 4); // shifting the rows to
    HSSFRow header = sheet.getRow(4);
    header.getCell(1).setCellValue("Test Group");
    header.getCell(2).setCellValue("Category");

    HSSFRow firstrow = sheet.getRow(0);
    firstrow.createCell(0).setCellValue("Actuals");

    SimpleDateFormat sdf = new SimpleDateFormat(
            "EEE MMM d HH:mm:ss 'CDT'  yyyy ");
    Date date = new Date();
    sdf.setTimeZone(TimeZone.getTimeZone("GMT-5"));
    String reportDate = sdf.format(date);
    HSSFRow thirdrow = sheet.getRow(3);
    thirdrow.createCell(0).setCellValue(reportDate);

    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont fontHeader = (HSSFFont) wb.createFont();
    fontHeader.setFontName("Calibri");
    cellStyle.setFont(fontHeader);
    System.out.println(" header.getPhysicalNumberOfCells();::::"
            + header.getPhysicalNumberOfCells());

    //only for changing font for header
    //for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
    //    HSSFCell cell = header.getCell(i);
    //    cell.setCellStyle(cellStyle);
        // sheet.setDefaultColumnStyle(i, cellStyle);

    //} you dont need this because the code bellow will change all style including header

    System.out.println("sheet.getLastRowNum():::" + sheet.getLastRowNum());
    for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
        HSSFRow row = sheet.getRow(j);
        if (row != null) {
            System.out.println(" j>>>" + j);
            // you can add sysout or debug here to check if all row passed
            // successfully
            for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                HSSFCell cell = row.getCell(i);
                if (cell != null) {
                    System.out.println(" i++" + i);
                    //HSSFCell celll = header.getCell(i); <<you also don't need this, this is the root of your problem, you choose to change header only instead of already declares cell
                    cell.setCellStyle(cellStyle);
                }
            }
        }
    }


}
查看更多
登录 后发表回答