通过poi来读取excel文件过程中,通过cell.getStringCellValue()方法尝试从一个数字类型的Cell读取出一个字符串时报错。
报错:
Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:845)
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:294)
at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:246)
at com.li.demo.utils.Service.upload(Service.java:39)
at com.li.demo.utils.Service.main(Service.java:20)
解决方法:
cell存在类型问题,可以在获取数据前先设置好cell的类型,如下:
if(row.getCell(0) != null) {
Cell cell = row.getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
}