8

I am using apache poi 3.8 to create an excel file. This excel file needs to contain some dates.

I am trying to write a date to the excel file with as format the excel type "date". But i always get a type "custom". I need to use the the type "date", so it will be localized based on the users settings.

I have tried the following:

Apache poi date format

Apache POI localized Date into Excel cell

But it doens't work.

This is the code that I have:

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");

XSSFDataFormat df = wb.createDataFormat();
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(df.getFormat("d-mmm-yy"));

XSSFCell cell = sheet.createRow(0).createCell(0);

Calendar c = Calendar.getInstance();
c.set(2012,3-1,18);
cell.setCellValue( c.getTime() );

cell.setCellStyle(cs);

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:\\temp\\dates-sworkbook.xlsx");
wb.write(fileOut);
fileOut.close();

Which format should I use?

Thanks

Community
  • 1
  • 1
cremersstijn
  • 2,375
  • 4
  • 28
  • 41

4 Answers4

10

Date is one of the special, built in formats. If you want to use that one explicitly, then you need to pick it explicitly. (Almost all formats used in Excel are custom ones, that render how you specify, but there are a few special ones)

POI has the full list of special, built in formats (which is much smaller than the list of formats in the dropdowns in recent copies of Excel!) in BuiltinFormats. If you make sure you set one of those, and not your own custom format string, it should behave exactly as you expect

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • 3
    I thought "d-mmm-yy" was a built in format. But for some reason this format doesn't work. When I use "m/d/yy" it works as expected. – cremersstijn Jun 29 '12 at 11:20
  • 1
    Nope, d-mmm-yy is a European style format. That's what things like the built in (American style) ones looks like when you display it in Europe! – Gagravarr Jun 29 '12 at 12:17
  • Actually, it's not true. I specified format 22 (0x16, "m/d/yy h:mm"). But it's shown as "Custom". – Alexey Oct 16 '15 at 11:19
4

Don't really know if you solved this already or not but here's a code that would solve it. It actually solved my problem.

CreationHelper createHelper = wb.getCreationHelper();
...
cs.setDataFormat(createHelper.createDataFormat().getFormat("yourformat"));

Taken from http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells

Lowb
  • 162
  • 1
  • 13
0

I understand your problem. I resolved using DateUtil of Apache Poi.

Cell cell = yourRow.createCell(yourIndex);
cell.setCellValue(DateUtil.getExcelDate(yourDate);

Enjoy !!

Marcus
  • 80
  • 6
0

It is resolved using below:

DataFormatter fmt = new DataFormatter();
Object Excel1data=Excel.readExcel(Excel1, "Sheet3", rownumber, columnnumber1);
            String valueAsInExcel1 = fmt.formatCellValue((Cell) Excel1data);

            Object Excel2Data=Excel.readExcel(Excel2, "Sheet2", getrownumber, columnnmumber2);
            String valueAsInExcel2 = fmt.formatCellValue((Cell) Excel2Data);

            org.testng.Assert.assertEquals(valueAsInExcel1, valueAsInExcel2,"Incorrect data in RowNumber:"+getrownumber+" ColumnNumber:"+columnnmumber2);
            columnnumber1++;
Arjun Marati
  • 397
  • 3
  • 3