JSF -Set global font type for the excel sheet in JSF using HSSFFont in Apache POI

孤者浪人 提交于 2019-12-23 04:22:28

问题


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:


回答1:


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);
                }
            }
        }
    }


}


来源:https://stackoverflow.com/questions/22910565/jsf-set-global-font-type-for-the-excel-sheet-in-jsf-using-hssffont-in-apache-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!