当前位置:网站首页>Excel导入异常Cannot get a STRING value from a NUMERIC cell解决
Excel导入异常Cannot get a STRING value from a NUMERIC cell解决
2022-08-06 05:27:00 【三省同学】
报错
导入Excel文件出现异常
Cannot get a STRING value from a NUMERIC cell
报错代码
String value = row.getCell(column).getStringCellValue();
Excel依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
解决
方案一
适用于POI 5.0以前,5.0版本后setCellType方法会被废弃。
Cell value = row.getCell(column);
if (StringUtils.isNotNull(value)) {
value.setCellType(CellType.STRING);
System.out.println(value.getStringCellValue());
}
方案二(推荐)
用于多种类型处理
先判断单元格类型,在根据类型获取值,这样就万无一失了
Cell cell = row.getCell(column);
if (StringUtils.isNotNull(cell))
{
if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
{
val = cell.getNumericCellValue();
if (DateUtil.isCellDateFormatted(cell))
{
// POI Excel 日期格式转换
val = DateUtil.getJavaDate((Double) val);
}
else
{
if ((Double) val % 1 != 0)
{
val = new BigDecimal(val.toString());
}
else
{
val = new DecimalFormat("0").format(val);
}
}
}
else if (cell.getCellType() == CellType.STRING)
{
val = cell.getStringCellValue();
}
else if (cell.getCellType() == CellType.BOOLEAN)
{
val = cell.getBooleanCellValue();
}
else if (cell.getCellType() == CellType.ERROR)
{
val = cell.getErrorCellValue();
}
}
复盘
报错源码信息提示:
Cannot get a STRING value from a NUMERIC cell
org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1035)
org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:390)
org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:342)
相关源码
/** * Used to help format error messages */
private static RuntimeException typeMismatch(CellType expectedType, CellType actualType, boolean isFormulaCell) {
String msg = "Cannot get a " + expectedType + " value from a " + actualType+ " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg);
}
@Deprecated
@Removal(version = "5.0")
void setCellType(CellType cellType);
扩展

Cell类的这三个实现有什么区别呢
HSSF:适用于Excel97-2003版本,07版本之前默认扩展名为.xls。
XSSF:适用于Excel2007及之后的版本,默认扩展名为.xlsx,向下兼容。
SXSSF:是在XSSF基础上,POI3.8版本开始提供的一种支持低内存占用的操作方式,扩展名为.xlsx。
XLSX格式的占用空间比XLS的小。XLSX是用新的基于XML的压缩文件格式取代了XLS默认文件格式。
台下人走过 不见旧颜色 台上人唱着 心碎离别歌
边栏推荐
猜你喜欢

Redis高可用、持久化及性能管理

【R语言】环境配置Anaconda + R4.1.3 + Pycharm

yolov4、yolov5训练nuscenes数据集/nuscenes数据集转coco格式

C语言和其他高级语言的最大的区别是什么?

简易数据库管理系统(DBMS)设计与实现

Zero foundation to build their own famine Don 't Starve server, get rid of the online caton and happy friend online

ELK日志分析系统(一)之ELK原理

Ad Hoc Query - Kylin

SemEval 2022 | 将知识引入NER系统,阿里达摩院获最佳论文奖

KDD 2022 | 中科院计算所提出STABLE:一种无监督高鲁棒性图结构学习框架
随机推荐
AI直接剪视频?哥本哈根大学提出基于CLIP的文本视频目标编辑模型
Quick图形旋转、缩放和平移
随机森林_R代码及用法
R语言处理时间序列数据
[R] language environment configuration Anaconda + R4.1.3 + Pycharm
新朋老友齐聚首,共话「图形学」未来 | 将门行动派特别直播企划,就在7月6日晚!
【多传感器融合】卡尔曼滤波
Qt 5.14.2 connect to Mysql database
GFS分布式文件系统
【多传感器融合】激光雷达与相机前融合
About the printf function Warning: format string is not a string literal (potentially insecure)!
R爬虫常用的包与用法
Cross-compilation libcurl + the openssl library
7月17日上午,阿里AE技术团队直播专场,分享CVPR挑战赛冠军、亚军方案!
Why is it difficult for the poor to turn over?
C语言常用的宏定义
AMPCOLOY940 high thermal conductivity beryllium-free copper alloy imported from the United States
List集合遍历的五种方法
ELK日志分析系统(一)之ELK原理
R语言模糊匹配