我正在尝试上传并读取 Excel 文件的内容。如果内容是字符串格式,则效果很好。但我的excel文件内容如下:
02:02:02
03:03:03
04:04:04
我想做的就是按原样检索内容。但是当我拉取这些数据时,我最终得到的内容如下:
8.4745370370370374E-2
0.12711805555555555
0.169490740740741
有什么办法可以不改变内容吗?
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
}
}
}
最佳答案
您的单元格格式可能未设置为文本。看看这个:
如您所见,当您没有显式将单元格类型设置为文本时,Excel 会尝试猜测它。因此,当您导入它时,它可能会作为时间导入。您可以通过将单元格类型显式设置为 Text
然后输入时间 02:02:02
将其读取为文本,或者您也可以将其读取为 excel 时间格式并然后转换它。了解有关读取 Excel 日期的更多信息:
How to read Excel cell having Date with Apache POI?
Reading date values from excel cell using POI HSSF API
关于java - 读取 Excel 文件而不更改其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36186807/