我正在使用Apache POI读取Excel文件。在读取时,我注意到它将字符串作为浮点值处理。
如果我的单元格包含1,则会将其提取为1.0
我从以前的问题中获取了一些提示,并修改了代码,但浮点表示仍然保持不变。
我应该如何正确读取字符串和日期数据?
DataFormatter df = new DataFormatter();
for (Row row : sheet) {
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("CELL: " + cn + " --> " + df.formatCellValue(cell));
if (row.getRowNum() == 0) {
sheetColumnNames.add(cell.getRichStringCellValue().getString());
}
}
}
df.formatCellValue(cell));
似乎是罪魁祸首... 请注释掉打印语句,然后再次检查。 - Sankumarsinghcell.setCellType(Cell.CELL_TYPE_STRING);
这里是不应该存在的。当你删去setCellType
这行代码后,DataFormatter
是否可以正确处理呢? - Gagravarr