代码之家  ›  专栏  ›  技术社区  ›  Joe

Primefaces dataExporter到xls浮点数成为电子表格单元格中的文本

  •  3
  • Joe  · 技术社区  · 6 年前

    环境:

    • jsf 2.2
    • primefaces 6.1
    • wilfly 10

    我正在尝试使用primefaces的dataExporter将dataTable导出到excel,但首先

    <p:commandButton id="btnExpExcel"
                     alt="#{msgs.inv_exportinvoices}"
                     ajax="false">
        <p:dataExporter type="xls" target="lstFactures" 
                        fileName="invoices"/>
    </p:commandButton>
    <p:dataTable id="lstFactures" var="inv"
    ...
    

    选项1 我是xls pex的。83.2但我们使用,作为十进制,而不是。

    ...
    <p:column headerText="#{msgs.total}">
        <h:outputText value="#{inv.total}">
            <f:convertNumber locale="#{localeBean.locale}"/>
        </h:outputText>
    </p:column>
    ...
    

    选项2 我是xls pex的。83,2但excel将其处理为文本而不是数字

    ...
    <p:column headerText="#{msgs.total}">
        <h:outputText value="#{inv.total}" />
    </p:column>
    ...
    

    **选项3**带

    public void postProcessXLS(对象文档){ HSSF工作手册wb=(HSSF工作手册)文件; HSSF表=wb。getSheetAt(0); HSSFRow收割台;

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        int ind = 0;
        for (int row = 0; row < invoices.size() + 1; row++) {
            header = sheet.getRow(row);
            for (int col = 0; col < header.getPhysicalNumberOfCells(); col++) {
                ...
                    }
                    if (col == 5) {
                        HSSFCell cell = header.getCell(col);
                        //Total is a float
                        cell.setCellValue(invoices.get(ind).getTotal());
                        ind++;
                    }
                }
            }
        }
    }
    

    我还尝试exportFunction=“\{inv.total}”,但出现了某种错误exportFunction=“\{inv.total}”:找不到方法。。。

    xls中的内容如下

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  4
  •   Luca G.    6 年前

    p:dataTable中的所有字段都导出为文本。 如果要将值转换为其他格式,则必须实现后处理器方法。

    例子:
    页xhtml

    <p:dataExporter type="xls" target="lstFactures" fileName="invoices" postProcessor="#{bean.ppMethod}" />
    

    类Bean

    public void ppMethod(Object document) {   
        Workbook workbook = (Workbook) document;
        ...
        CellStyle totalCellStyle = workbook.createCellStyle(); 
    
        totalCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00"));
    
        Cell currentCell = workbook.getSheetAt(0).getRow(0).getCell(0);
    
    
        currentCell.setCellValue(Double.parseDouble(currentCell.getStringCellValue()));
        currentCell.setCellStyle(defaultCellStyle);
        ...
    }