简体   繁体   English

如何使用Java从Excel中获取单元格,无论该单元格可能包含字符串还是数字值?

[英]how to fetch a cell from excel using java whether the cell may contains a string or numeric value?

Here is my code: 这是我的代码:

long tin=0; 
tin = (long)row.getCell(10).getNumericCellValue();

tin value of excel may contains string or only numeric value.. if tin value is numeric am able to fetch data.. if tin value is String am getting error. excel的tin值可能包含字符串或仅包含数字值。.如果tin值是数字,则能够获取数据。.如果tin值是String,则报错。

You can check the type of the cell using Cell#getCellTypeEnum() . 您可以使用Cell#getCellTypeEnum()检查单元格的类型。 Possible returned values are: 可能返回的值是:

  • _NONE - Unknown type, used to represent a state prior to initialization or the lack of a concrete type. _NONE-未知类型,用于表示初始化之前的状态或缺少具体类型。
  • BLANK - Blank cell type 空白-空白单元格类型
  • BOOLEAN - Boolean cell type BOOLEAN-布尔单元格类型
  • ERROR - Error cell type 错误-错误单元格类型
  • FORMULA - Formula cell type 公式-公式单元格类型
  • NUMERIC - Numeric cell type (whole numbers, fractional numbers, dates) NUMERIC-数字单元格类型(整数,小数,日期)
  • STRING - String (text) cell type STRING-字符串(文本)单元格类型

In other words you should try the following scaffold: 换句话说,您应该尝试以下支架:

tin = row.getCell(10).getCellTypeEnum().equals(CellType.NUMERIC) ? 
     (long)row.getCell(10).getNumericCellValue() :
     Long.parseLong(row.getCell(10).getStringCellValue());

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

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