我正在尝试使用apache poi将xls和xlsx文件转换为csv,但遇到了某种我不知道如何解决的问题。基本上,当一个单元格包含一个大数字时,代码会将其输出为怪异的格式。

for (int cn = 0; cn < lastColumn; cn++) {
    Cell c = r.getCell(cn);
    if (c == null) {
        sb.append("|");
    } else {
        sb.append(c + "|");
    }
}


例:

电子表格在一个单元格中包含此数字:84172870494

输出是这个:8.4172870494E10

所需的输出:84172870494

基本上,它添加一个“。”和“ E%”结尾。

最佳答案

您必须定义格式才能告诉POI如何解析数据。

DataFormat format = wb.createDataFormat();
CellStyle style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#.###############"));
cell.setCellStyle(style);


并检查xls格式:

https://poi.apache.org/components/spreadsheet/quick-guide.html#DataFormats
https://support.microsoft.com/en-us/help/264372/how-to-control-and-understand-settings-in-the-format-cells-dialog-box

09-30 14:52
查看更多