简体   繁体   中英

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.

You can check the type of the cell using Cell#getCellTypeEnum() . Possible returned values are:

  • _NONE - Unknown type, used to represent a state prior to initialization or the lack of a concrete type.
  • BLANK - Blank cell type
  • BOOLEAN - Boolean cell type
  • ERROR - Error cell type
  • FORMULA - Formula cell type
  • NUMERIC - Numeric cell type (whole numbers, fractional numbers, dates)
  • STRING - String (text) cell type

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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