excel - 如何覆盖 PrimeFaces p :dataExporter wrongly exporting numbers as text in Excel?

标签 excel jsf primefaces apache-poi

PrimeFace p:dataExporter默认情况下,标记将数字数据导出为文本,这会导致单元格的左上角带有绿色三角形。这可以在 PrimeFaces showcase example 中看到同样,如果您单击汽车表下的 Excel 导出。

如何覆盖此默认值以确保我的数字列不会导出为文本?我尝试使用 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);

        }
    }
}

我也试过

cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

但这给了我

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

这意味着所有数据都被不加区别地导出为文本,之后您甚至无法更改它。

最佳答案

这就是最终对我有用的东西。它远非优雅,但它有效:

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

关于excel - 如何覆盖 PrimeFaces p :dataExporter wrongly exporting numbers as text in Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110104/

相关文章:

VBA 从功能区中删除自定义工具栏 Excel 2013

java - 为什么这次调用中的服务为空?

jsf - p :ajax is crashing composite component

jsf-2 - 带有复选框的 Primefaces 树,在单击行上的任意位置时避免选中/取消选中

jsf - 在 Primefaces 表行选择的基础上动态启用/禁用命令按钮

vba - Excel VBA中的 "Name"是什么意思

excel - ExecuteExcel4Macro,包含撇号的路径

excel - 插入形状逐渐变慢

css - 如何在 Primefaces 5.3 中重新设置单个 View 的样式

java - JSF HttpSession 登录传递参数?