Java POI - read date from Excel file
Dates cannot be stored in CELL_TYPE_STRING
cell. You should store it in CELL_TYPE_NUMERIC
cell. See here for details.
You also missed break
keyword after first case
. So if cell is Cell.CELL_TYPE_STRING
then also
System.out.print(cell.getNumericCellValue() + "\t\t");
is called.
So it should be:
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
System.out.print(dateFormat.format(cell.getDateCellValue()) + "\t\t");
} else {
System.out.print(cell.getNumericCellValue() + "\t\t");
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
}
This is direct pick from Apache POI tutorial, you make like to visit and get more details.
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println();
}
Formatting date: This thread may answer your follow up question.