线程“main”中的异常 java.lang.IllegalStateException:无法从数字单元格获取文本值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24529486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell
提问by user2323844
I have written code as below to fetch value from Excel.
我编写了如下代码来从 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 :
但在 excel 中,第一个单元格是一个数值,但在上面的代码中,我试图获取字符串单元格值。这就是为什么我收到错误消息:
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?
任何人都可以为此提供解决方案。如何从excel中获取数值?
回答by Sk1X1
I'm not sure, but I think that exists getNumericalCellValue()
or getIntegerCellValue()
which returns what you want.
我不确定,但我认为存在getNumericalCellValue()
或getIntegerCellValue()
返回你想要的。
回答by myxlptlk
getCellType() for any cell gives you the type of the cell.The types are:
任何单元格的 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
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.
最好使用 switch 语句并收集正确类型的单元格值。存在 getNumericCellValue() 和 getStringCellValue() 函数,但使用类型更安全。
回答by user2323844
Add apostrophe as prefix to that number in cell, automatically it will be converted as text. it had worked for me
在单元格中为该数字添加撇号作为前缀,它将自动转换为文本。它对我有用
回答by sina
Check the cell type first and then get its value.
首先检查单元格类型,然后获取其值。
row.getCell(index).getCellType();
Number ==> row.getCell(index).getNumericCellValue();
String ==> row.getCell(index).getStringCellValue();
回答by ?mer Faruk UYAR
You should use it =>
你应该使用它 =>
String CID = new BigDecimal(s1.getRow(i).getCell(0).getNumericCellValue()).toString();
回答by Bablu Gope
String unitPrice = new Float(wb.getSheetAt(0).getRow(0).getCell(7).getNumericCellValue()).toString();
String unitPrice = new Float(wb.getSheetAt(0).getRow(0).getCell(7).getNumericCellValue()).toString();
Note: its the best way to get the data in string if the value is stored in the numerical type in excel file. where we are getting the data from 7th cell of 1st row.
注意:如果值以数字类型存储在excel文件中,它是获取字符串数据的最佳方法。我们从第一行的第 7 个单元格获取数据的地方。