user6141031
user6141031

Reputation:

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.

Upvotes: 1

Views: 623

Answers (1)

xenteros
xenteros

Reputation: 15842

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

Upvotes: 1

Related Questions