I've a Java code which displays the contents of excel sheet. Now, when i'm working with numeric/string/boolean data it always gives me right result. Unlike, for testing my code i made few changes in the Excel and added a date value for example 4-sep-09. On running my code, it returned undesired results. Code and the o/p is shown as follows. How i can read and display date values from excel sheet? I'm using Apache Poi. I could have done something like XSSFDateUtil
and check if the cell value is date or not using if-else
? is that a possible solution?
Java Code:
FileInputStream file = new FileInputStream(new File(FileName));
System.out.println("The contents of sheet are:\n");
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
O/P i'm getting
ColA | ColB....(few other columns)
32755.0|(other values)
Instead what i want that date value must be printed instead of number. My sheet is having 4-SEP-89 date under ColA and it is displaying some numbers.
Desired O/P
ColA | ColB....(few other columns)
4-SEP-89|(other values)
Note:- this is part of my code, which is running fine. Its giving no errors, however i need small change in the o/p as asked above. i've been working on this for a while. i've asked questions related to displaying excel sheets before here- Issue while reading Excel document (Java code) which is resolved along with couple of others. Thanks anyway.
Also, as far as i think, everything from excel, is read as a String (except boolean values)? Isn't that right?