首页 > 其他分享 >Excel导入数据异常Cannot get a text value from a numeric cell解决办法

Excel导入数据异常Cannot get a text value from a numeric cell解决办法

时间:2022-10-15 14:26:02浏览次数:53  
标签:get text Excel value Cell Cannot getCell row

POI操作Excel时偶尔会出现Cannot get a text value from a numeric cell的异常错误。

异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric cell的异常错误。

此异常常见于类似如下代码中:row.getCell(0).getStringCellValue();

解决办法:先设置Cell的类型,然后就可以把纯数字作为String类型读进来了:

if(row.getCell(0) != null){
    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
    String value = row.getCell(0).getStringCellValue();
}

或者只判断数字:

if(row.getCell(0) != null && row.getCell(0) == Cell.CELL_TYPE_NUMERIC){
    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
    String value = row.getCell(0).getStringCellValue();
}

 

标签:get,text,Excel,value,Cell,Cannot,getCell,row
From: https://www.cnblogs.com/hunttown/p/16794119.html

相关文章