POI导入Excel异常Cannot get a text value from a numeric cell

问题

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

java.lang.IllegalStateException: Cannot get a text value from a numeric cell

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

解决

通过setCellType()设置cell类型,统一设置成String类型,然后再获取

Row row = sheet.getRow(i);
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
	// 
	row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
	// 
	String cellValue = row.getCell(j).getStringCellValue();
	System.out.println(cellValue);
}
posted @ 2019-04-23 17:37  tiancixiong  阅读(33)  评论(0)    收藏  举报