Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell

I have written code as below to fetch value from Excel.

String CID = s1.getRow(i).getCell(0).getStringCellValue();

but in excel, 1st cell is a numeric value, but in above code I am trying to fetch String cell value. thats why I am getting error as :

Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell

Can anyone please provide a solution for this. how to fetch the numeric value from excel?


Solution 1:

getCellType() for any cell gives you the type of the cell.The types are:

Cell.CELL_TYPE_BLANK
Cell.CELL_TYPE_NUMERIC
Cell.CELL_TYPE_STRING
Cell.CELL_TYPE_FORMULA
Cell.CELL_TYPE_BOOLEAN
Cell.CELL_TYPE_ERROR

It is better to use a switch statement and collect the correct type of cell value. There exists getNumericCellValue() and getStringCellValue() functions but it is safer with types.