java - 读取 Excel 文件而不更改其内容

标签 java excel

我正在尝试上传并读取 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;
            }
        }
    }

最佳答案

您的单元格格式可能未设置为文本。看看这个:

Cell type custom

Cell Type Text

如您所见,当您没有显式将单元格类型设置为文本时,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/

相关文章:

java - 为什么 Class.getClass() 可以不同于 Class.cast() 返回类型?

excel - On Error Goto 0 不重置错误捕获

python - 如何使用 Pandas 只读取 excel 标题?

javascript - 从当前行向后查找第一个非空值扫描行

vba - 将 Maximo 工单信息自动获取到 Excel

excel - VBA - 从另一个工作簿中关闭或单击 MsgBox 中的确定,

java - 当 preferredSize 为非默认值时,JButton 不执行操作的名称

java - 方法 get(int) 未为类型 Set 定义

java - GET 请求不适用于 Chrome --> net::ERR_CONTENT_LENGTH_MISMATCH

java - 当他们说 StringBuffer 类是同步的时,到底是什么意思?该类的所有方法都声明为同步的吗?