简体   繁体   English

如何覆盖PrimeFaces p:dataExporter在Excel中错误地将数字导出为文本?

[英]How to override PrimeFaces p:dataExporter wrongly exporting numbers as text in Excel?

The PrimeFace p:dataExporter tag exports numeric data as text by default, which results in a cell with a green triangle in the upper left corner. 默认情况下, PrimeFace p:dataExporter标记将数字数据导出为文本,这将导致单元格的左上角带有绿色三角形。 This can be seen in the PrimeFaces showcase example as well, if you click the Excel export under the cars table. 如果您单击cars表下的Excel导出,这也可以在PrimeFaces展示示例中看到。

How can I override this default to make sure my numeric columns are not exported as text? 如何覆盖此默认值以确保我的数字列不导出为文本? I tried using the postProcessor attribute pointing to my method that sets the Excel format for all the data cells using POI API but that did not take effect (did not change anything): 我尝试使用postProcessor属性来指向我的方法,该方法使用POI API为所有数据单元格设置Excel格式,但没有生效(未进行任何更改):

public void formatExcel(Object doc) {
    HSSFWorkbook book = (HSSFWorkbook)doc;
    HSSFSheet sheet = book.getSheetAt(0); 

    HSSFRow header = sheet.getRow(0);
    int colCount = header.getPhysicalNumberOfCells();
    int rowCount = sheet.getPhysicalNumberOfRows();

    HSSFCellStyle numStyle = book.createCellStyle();
    numStyle.setDataFormat((short)1);

    for(int rowInd = 1; rowInd < rowCount; rowInd++) {
        HSSFRow row = sheet.getRow(rowInd);
        for(int cellInd = 1; cellInd < colCount; cellInd++) {
            HSSFCell cell = row.getCell(cellInd);
            String val = cell.getStringCellValue();

            cell.setCellStyle(numStyle);

        }
    }
}

I also tried 我也试过

cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

but that gives me 但这给了我

java.lang.IllegalStateException: Cannot get a numeric value from a text cell

So that means that all data is indiscriminately exported as text and then you can't even change it afterwards. 因此,这意味着所有数据都被随意导出为文本,然后您甚至都无法对其进行更改。

This is what ended up working for me. 这就是最终为我工作的原因。 It is far from elegant but it works: 它远非优雅,但有效:

HSSFCellStyle intStyle = book.createCellStyle();
intStyle.setDataFormat((short)1);

HSSFCellStyle decStyle = book.createCellStyle();
decStyle.setDataFormat((short)2);

HSSFCellStyle dollarStyle = book.createCellStyle();
dollarStyle.setDataFormat((short)5);


for(int rowInd = 1; rowInd < rowCount; rowInd++) {
    HSSFRow row = sheet.getRow(rowInd);
    for(int cellInd = 1; cellInd < colCount; cellInd++) {
        HSSFCell cell = row.getCell(cellInd);

        //This is sortof a hack to counter PF exporting all data as text
        //We capture the existing value as string, convert to int, 
        //then format the cell to be numeric and reset the value to be int
        String strVal = cell.getStringCellValue();

        //this has to be done to temporarily blank out the cell value
        //because setting the type to numeric directly will cause 
        //an IllegalStateException because POI stupidly thinks
        //the cell is text because it was exported as such by PF...
        cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

        strVal = strVal.replace(",", StringUtils.EMPTY);

        if(strVal.indexOf('.') == -1) {
            //integer
            //numStyle.setDataFormat((short)1);
            int intVal = Integer.valueOf(strVal);

            cell.setCellStyle(intStyle);
            cell.setCellValue(intVal);
        } else {
            //double
            if(strVal.startsWith("$")) {
                strVal = strVal.replace("$", StringUtils.EMPTY);
                //numStyle.setDataFormat((short)5);
                cell.setCellStyle(dollarStyle);
            } else {
                //numStyle.setDataFormat((short)2);
                cell.setCellStyle(decStyle);
            }

            double dblVal = Double.valueOf(strVal);
            cell.setCellValue(dblVal);
        }
    }
}

In your postProcessor, you nowhere set the value of the cell to an integer. 在postProcessor中,您无处将单元格的值设置为整数。 You set the type, but not the value. 您设置类型,但不设置值。 Setting the type is not enough. 设置类型还不够。 You have to convert value to a number and set it again 您必须将值转换为数字并再次设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Primefaces <p:dataExporter> 不产生Excel下载 - Primefaces <p:dataExporter> does not produce Excel download 将条件添加到p:dataExporter的目标属性中,以一次导出两个不同选项卡中的两个数据表,从而在素字方面表现出色 - Adding condition into target attribute for p:dataExporter to export two datatables in two different tabs one at a time to excel in primefaces 将dataExporter填入xls Float数字成为电子表格单元格中的文本 - Primefaces dataExporter to xls Float number becomes text in spreadsheet cell Primefaces DataExporter-XLSX和XLSXSTREAM - Primefaces DataExporter - XLSX and XLSXSTREAM PrimeFaces DataExporter用于大数据 - PrimeFaces DataExporter for big data 使用软件包xlsx导出到excel时存储为文本的数字 - numbers stored as text when exporting to excel using package xlsx Excel 错误读取 csv 中的科学格式数字 - Excel wrongly read scientific formatted numbers in csv 在 Power Query 中将格式错误的文本转换为数字 - Transform Text Wrongly Formatted to Numbers in Power Query 通过VBA将Excel数据导出到文本文件时处理指数和格式 - Handling exponential numbers and formatting when exporting excel data via VBA to text file 如何在导出到excel之后创建永远不会转换为日期的文本? - How to make text that never be converted to date after exporting to excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM