Cannot get a text value from a numeric cell异常处理
问题背景:
使用POI读取Excel时出现Cannot get a text value from a numeric cell
的异常错误。
for (Row row : sheetAt) {
for (Cell cell : row) {
System.out.println(cell.getStringCellValue());
}
System.out.println("--------");
}
原因
Excel数据Cell有不同的类型,当Excel中有不同的数据类型,在读取一个数字类型的Cell并写入数据库时,就会出现Cannot get a text value from a numeric cell的异常错误。
处理方案
需要给cell设置type,添加如下内容
cell.setCellType(Cell.CELL_TYPE_STRING);
for (Row row : sheetAt) {
for (Cell cell : row) {
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println(cell.getStringCellValue());
}
System.out.println("--------");
}
还没有评论,来说两句吧...