Get actual cell value of excel cell with POI without defining a datatype

前端 未结 2 1317
渐次进展
渐次进展 2021-01-22 00:51

I catch my cells from an .xls file like this:

cell.getStringCellValue();

But since some of the cells are numeric, I have to do this instead:

相关标签:
2条回答
  • 2021-01-22 01:38

    You can use Apache POI DataFormatter's formatCellValue(Cell cell) method as it returns the formatted value of a cell as a String regardless of the cell type.

    According to DataFormatter doc -

    DataFormatter contains methods for formatting the value stored in an Cell. This can be useful for reports and GUI presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN, percentages, decimals, dates, phone numbers, zip codes, etc.

    Sample code

    DataFormatter dataFormatter= new DataFormatter();
    
    // Inside loop
    String cellValueStr = dataFormatter.formatCellValue(cell);
    
    0 讨论(0)
  • 2021-01-22 01:41

    try using cell.getCellType() as you have

    1. numeric
    2. date
    3. numeric in scientific notation
    0 讨论(0)
提交回复
热议问题