1

I have used Apache poi DataFormatter class for getting the date same as given in excel sheet. But in the below case, it is failing.

For example, if I give the date as 19/9/2017, it is getting as 19/9/17.

I have used below code to get the date value same as given in excel sheet.

DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();
chitra
  • 5
  • 1
  • 2
  • 2
    If the cell, which is containing the date, is formatted as the default date format (Short Date), then only the format id 0xE (14) is stored in the file. So `DataFormatter` has no chance to determine how a special `Excel` will show this date, since there is not a DataFormatString. See https://stackoverflow.com/questions/34900605/excell-cell-style-issue/34902174#34902174. – Axel Richter May 16 '17 at 14:21

1 Answers1

3
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);  
XSSFRow row = sheet.getRow(0);
XSSFCell cell = row.getCell((short)0);
short x = wb.createDataFormat().getFormat("dd/m/yyyy;@");
CellStyle dateCellFormat = wb.createCellStyle();
dateCellFormat.setDataFormat(x);

cell.setCellStyle(dateCellFormat);
DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();
System.out.println(stringCellValue);

This code prints 19/12/2017 for input 19/12/2017 and 19/9/2017 for 19/09/2017. Maybe you can adapt my code to get yours to work.

alexrnov
  • 2,346
  • 3
  • 18
  • 34
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65