如何使用 Primefaces 中的自定义数据导出器在 excel 中设置字体?

How to set font in excel using customized data exporter in Primefaces?

我正在使用 primefaces 6.1。我需要将我的报告列表导出到 excel format.For 我使用的 primefaces 数据 exporter.Dataexporter 给我 excel 格式。我还在 excel 中设置了字体。但是字体在我的excelfile.The设置中不完全起作用,字体出现在tableheader而已。 我的代码是这样的:

public void postProcessXLS(Object document) {
    HSSFWorkbook wb = (HSSFWorkbook) document;
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow header = sheet.getRow(0);

    HSSFCellStyle cellStyle = wb.createCellStyle();  
    cellStyle.setFillForegroundColor(HSSFColor.AQUA.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont fontHeader = (HSSFFont) wb.createFont();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    fontHeader.setFontName("Magnus Nepali");
    cellStyle.setFont(fontHeader);
    for(int i=0; i < header.getPhysicalNumberOfCells();i++) {
        HSSFCell cell = header.getCell(i);

        cell.setCellStyle(cellStyle);

    }
}

我的按钮代码是:

<p:commandButton id="excel" ajax="false">
    <p:dataExporter type="xls" target="tblReport" fileName="report" postProcessor="#{shareDividendMB.postProcessXLS}" />
</p:commandButton>

我的excel格式是这样的:

只有 excel sheet header 已更改为定义的字体,其余数据已尝试使用 Arial font.I 除了我的字体之外,结果相同。

最后我得到了 solution.My 工作代码是这样的:

 public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFCellStyle cellStyle = wb.createCellStyle();  
        cellStyle.setFillForegroundColor(HSSFColor.AQUA.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont fontHeader = (HSSFFont) wb.createFont();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        fontHeader.setFontName("Magnus Nepali");
        cellStyle.setFont(fontHeader);
        for (Row row : sheet) {
            for (Cell cell : row) {
                cell.setCellStyle(cellStyle);
            }
        }

    }