我编写了如下代码来从 Excel 中获取值。
String CID = s1.getRow(i).getCell(0).getStringCellValue();
但在 Excel 中,第一个单元格是一个数值,但在上面的代码中我试图获取字符串单元格值。这就是为什么我收到错误:
Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell
任何人都可以为此提供解决方案吗?如何从Excel中获取数值?
最佳答案
任何单元格的 getCellType() 都会为您提供单元格的类型。类型为:
Cell.CELL_TYPE_BLANK
Cell.CELL_TYPE_NUMERIC
Cell.CELL_TYPE_STRING
Cell.CELL_TYPE_FORMULA
Cell.CELL_TYPE_BOOLEAN
Cell.CELL_TYPE_ERROR
最好使用 switch 语句并收集正确类型的单元格值。 存在 getNumericCellValue() 和 getStringCellValue() 函数,但使用类型更安全。
关于java - 线程中的异常 "main"java.lang.IllegalStateException : Cannot get a text value from a numeric cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24529486/