线程“main"中的异常java.lang.IllegalStateException:无法从数字单元格中获取文本值 [英] Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell

查看:32
本文介绍了线程“main"中的异常java.lang.IllegalStateException:无法从数字单元格中获取文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了如下代码来从 Excel 中获取值.

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

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

但在 excel 中,第一个单元格是一个数值,但在上面的代码中,我试图获取 String 单元格值.这就是为什么我收到错误:

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

任何人都可以为此提供解决方案.如何从excel中获取数值?

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

推荐答案

getCellType() for any cell 给你单元格的类型.类型是:

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

最好使用 switch 语句并收集正确类型的单元格值.存在 getNumericCellValue() 和 getStringCellValue() 函数,但使用类型更安全.

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.

这篇关于线程“main"中的异常java.lang.IllegalStateException:无法从数字单元格中获取文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆