I'm using Apache POI and trying to add a date format style to a numeric field in my spreadsheet. But when I add a style to my Cell it fails to open in Excel (tries to repair the file).
My code looks pretty much like this:
DeferredSXSSFWorkbook wb = new DeferredSXSSFWorkbook(100);
DeferredSXSSFSheet sheet1 = wb.createSheet("Sheet1");
...
Row row = sheet.createRow(0);
CellStyle style = sheet.getWorkbook().createCellStyle();
CreationHelper createHelper = sheet.getWorkbook().getCreationHelper();
short format = createHelper.createDataFormat().getFormat("m/d/yy");
style.setDataFormat(format);
...
Cell cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(new Date());
I was able to unzip the xlsx file and get the generated sheet1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<dimension ref="A1"/>
<sheetViews>
<sheetView tabSelected="true" workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="15.0"/>
<sheetData>
<row r="2">
<c r="A2" s="1" t="n">
<v>45442.657083680555</v>
</c>
</row>
</sheetData>
<pageMargins bottom="0.75" footer="0.3" header="0.3" left="0.7" right="0.7" top="0.75"/>
</worksheet>
And styles.xml:
<?xml version="1.0" encoding="UTF-8"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<numFmts count="0"/>
<fonts count="1">
<font>
<sz val="11.0"/>
<color indexed="8"/>
<name val="Calibri"/>
<family val="2"/>
<scheme val="minor"/>
</font>
</fonts>
<fills count="2">
<fill>
<patternFill patternType="none"/>
</fill>
<fill>
<patternFill patternType="darkGray"/>
</fill>
</fills>
<borders count="1">
<border>
<left/>
<right/>
<top/>
<bottom/>
<diagonal/>
</border>
</borders>
<cellStyleXfs count="1">
<xf borderId="0" fillId="0" fontId="0" numFmtId="0"/>
</cellStyleXfs>
<cellXfs count="1">
<xf borderId="0" fillId="0" fontId="0" numFmtId="0" xfId="0"/>
</cellXfs>
</styleSheet>
If I'm understanding the problem, s="1" on the cell should be referencing the date format style? However, I don't see any style that looks like the one I created in styles.xml. I should be expecting something more inside of cellStyleXfs, right?
I checked lots of examples and the Apache documentation but I'm not seeing what I'm doing wrong. Anyone run into this before or have any ideas?
...in your code sample? Why not providing a complete example? To get proper answers, complete examples are needed. Else there is much guessing and suspecting.