【发布时间】:2013-12-06 18:20:30
【问题描述】:
我正在使用 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));是罪魁祸首...请评论打印语句,然后再检查。 -
我认为问题在于不应该存在的
cell.setCellType(Cell.CELL_TYPE_STRING);。当您删除setCellType行时,DataFormatter是否正确? -
移除了二传手。它运作良好。谢谢
标签: java excel apache-poi