0
votes

I'm using apache POI for reading excel.I have one date field in my excel file with date format as "MM/dd/yy". I would like to read all the cells as String. so I set all the cell type to string by using "cell.setCellType(Cell.CELL_TYPE_STRING);". When this cell is converted into String the value i see for the date field is 41886. Please let me know if I'm missing anything.

Thank you.

3

3 Answers

3
votes

The string 41886 measures time relative to the Excel epoch. Instead, leave the type as Cell.CELL_TYPE_NUMERIC and use DateUtil as shown here.

if (DateUtil.isCellDateFormatted(cell)) {
    System.out.print(cell.getDateCellValue());
}
3
votes

Use the method .formatCellValue(cell) to return the formatted value of the cell as a String this method can be found in the DataFormatter class

more information in the documentation:

DateFormatter (POI Documentations)

2
votes

Dates in Excel are stored as a floating point value representing number of days (and fraction of day) since some epoch date (I think it's in 1900). You need to read the date value as a date and do the conversion in Java.