简体   繁体   English

错误:“无法从 NUMERIC 单元格中获取 STRING 值”,在转换 Excel 时

[英]Error: "Cannot get a STRING value from a NUMERIC cell", when converting Excel

I have excel sheet with date column, when I run my program I got this error:我有带日期列的 excel 表,当我运行我的程序时出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

My code that I run:我运行的代码:

String date =sheets.getRow(choosenRow).getCell(3).getStringCellValue().toString();

How can I convert numeric value to String?如何将数值转换为字符串? in my case I used .toString();就我而言,我使用了.toString(); but seems it didn't worked.但似乎没有用。

I believe that it is because you are using .getStringCellValue() , when you should be using getNumericCellValue() .我相信这是因为您正在使用.getStringCellValue() ,而您应该使用getNumericCellValue() .getStringCellValue() can't be used with NUMERIC cells. .getStringCellValue()不能与 NUMERIC 单元格一起使用。 I'm assuming that you are using Apache POI by the way.顺便说一下,我假设您正在使用 Apache POI。

See https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html for more info.有关更多信息,请参阅https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

I'm not sure why I was downvoted, can you let me know in the comments about how I can rewrite it to be more clear(if that is the problem) or tell me how it is wrong?我不确定为什么我被否决了,您能否在评论中告诉我如何重写它以使其更清晰(如果这是问题)或告诉我它是如何错误的?

i not tested your code..!我没有测试你的代码..!

but

you should try Object class to get data from that exal file.您应该尝试Object类从该 exal 文件中获取数据。 if data are successfully recevied.如果数据成功接收。 then you can convert in to date or String.然后您可以转换为日期或字符串。

like :喜欢 :

Object objdata = sheets.getRow(choosenRow).getCell(3).getStringCellValue();

Thanks all, it's working perfectly now.谢谢大家,它现在工作得很好。 I have changed the code to be like that:我已将代码更改为如下所示:

Date date =sheets.getRow(choosenRow).getCell(3)getDateCellValue();
dateTextField.setText(""+date);

The error tells you that the cell if type NUMERIC , you may use the approriate method getNumericCellValue()该错误告诉您,如果单元格类型为NUMERIC ,您可以使用适当的方法getNumericCellValue()

String date = "" + sheets.getRow(choosenRow).getCell(3).getNumericCellValue();

If you don't know the type before, and need to read some values, you may implment a switch on the type ( getCellType() ) and from the result of this enum use the right method如果您之前不知道类型,并且需要读取一些值,您可以在类型( getCellType() )上实现一个switch ,并从这个枚举的结果中使用正确的方法

Specifically for the numeric type, you can check for date element :特别是对于数字类型,您可以检查日期元素:

public String getContent(Cell c){
    switch(c.getCellType()){
        case: NONE     :  throw new IllegalArgumentException("NONE");
        case: BLANK    :  return "";
        case: BOOLEAN  :  return ""+c.getBooleanCellValue();
        case: ERROR    :  return ""+c.getErrorCellValue()
        case: FORMULA  :  return c.getCellFormula()
        case: NUMERIC  :  return HSSFDateUtil.isCellDateFormatted(c)) ? 
                                 c.getDateCellValue()) : ""+c.getNumericCellValue();
        case: STRING   :  return c.getStringCellValue()
}

Ref to How to read Excel cell having Date with Apache POI?参考如何使用 Apache POI 读取具有日期的 Excel 单元格?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM