简体   繁体   中英

Primefaces dataExporter to xls Float number becomes text in spreadsheet cell

Environment:

  • jsf 2.2
  • primefaces 6.1
  • wilfly 10

I'm trying to export a dataTable to an excel with dataExporter from primefaces, but I'm firstly getting

<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"
...

Option 1 I get in xls pex. 83.2 but we use , as decimal instead of .

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

Option 2 I get in xls pex. 83,2 but excel deal with that as text instead of number

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

**Option3 ** with

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

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

I also tried to exportFuction="#{inv.total}" but I got some kind of error exportFunction="#{inv.total}": Method not found...

What I'm getting in xls is the following

在此输入图像描述

All the fields in p:dataTable are exported as text. If you want to convert a value in a different format, you have to implement a postProcessor method.

Example:
page.xhtml

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM