[Solved] java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

Error Message:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
 at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1050)
 at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:404)
 at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:70)
 at com.jawasoft.testDemo.ExcelUtil.readExcel(ExcelUtil.java:81)
 at com.jawasoft.testDemo.ExcelUtil.readExcelToObj(ExcelUtil.java:46)
 at com.jawasoft.testDemo.ExcelUtil.main(ExcelUtil.java:29)

Reason for error: When reading the cell string, there is data of type number, so it needs to be converted into pure String type, so that no error will be reported.

Error code:

returnStr = cell.getRichStringCellValue().getString();

or the following code

// Get cell data 
String cellValue = cell.getStringCellValue();

Solution: Set the cell type to String before reading the excel cell data conversion

code show as below:

//set cell type
cell.setCellType(CellType.STRING); 
returnStr = cell.getRichStringCellValue().getString();

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *