场景描述:读取excel中数据时,“123”这类数据被默认读取成了“123.0”。之前遇到过,解决后,时隔多日,忘记了上次的解决,特此记录下。
解决方法:
使用dataFormatter.formatCellValue(row.getCell(0))取数
DataFormatter dataFormatter = new DataFormatter();
String htbh = row.getCell(0) == null ? "" : dataFormatter.formatCellValue(row.getCell(0));
题外话:
第一次遇到时通过上述方法解决了,但是第二次时因为时间久了忘记了解决方法,使用了
r.getCell(0).setCellType(CELLTYPE.STRING);
但是该方法在5.0版本已经被弃用了

原因是设置数据格式,例如单元格里存了整形,直接用字符串接收时就报错了。

解决Excel读取整数变为小数问题
在读取Excel中的数据时,数值如123被错误地读为123.0。为了解决这个问题,可以使用DataFormatter类的formatCellValue方法来获取格式化后的值。先前尝试过设置单元格类型为STRING,但该方法在5.0版本已被弃用。使用dataFormatter.formatCellValue能正确处理整数,避免数据类型转换错误。
9606

被折叠的 条评论
为什么被折叠?



