0

I needed to get cell data from Excel files as they look like and bumped to DataFormatter class of Apache PO. This works like a charm except for cells containing date. Below is my code:

while (rowIterator.hasNext())
{
    Row row = rowIterator.next();

    StringBuilder rowDataBuilder = new StringBuilder();
    int iCellCount = row.getLastCellNum();
    for (int i = 0; i < iCellCount; i++)
    {
        Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
        rowDataBuilder.append(dataFormatter.formatCellValue(cell));
        rowDataBuilder.append(" | ");
    }

    _LOG.info("-----> row data: " + rowDataBuilder.toString());
}

For example, a cell contains 5/3/2013, I only get 5/3/13. Would there be any solutions for this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Oneb
  • 375
  • 1
  • 10
  • 21
  • Are you sure your dates are really set to `dd/mm/yyyy` format? Or are they using one of the default "localise-me" formats? (The way to test is to open on a US copy of Excel, and see what shows up) – Gagravarr Jun 03 '13 at 07:48
  • well when I open the excel file it shows d/m/yyyy but I assume it is dd/mm/yyyy. I am not sure if I am opening a US copy. – Oneb Jun 03 '13 at 08:22

1 Answers1

3

For fetching the date in desired format you can use following:

  SimpleDateFormat DtFormat = new SimpleDateFormat("dd/MM/yyyy");
  Date date=Test.getRow(RowNum).getCell(CellNum).getDateCellValue();
  System.out.println(DtFormat.format(date).toString());

And now if the cell value is 05/03/2013, it will give o/p as 05/03/2013. I hope this will resolve your problem.

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74