I am creating a new .xlsx-File and very time i open it for the first time and close it, this annoying popup comes up. this project should create a Excel-File, looking like something my coworker did by hand and the xml-File I post down there, is from one of his files. My code where i save the file is working fine:
private File save() throws IOException {
Date now = new Date();
String userHome = System.getProperty("user.home");
for (int fCounter = -1;; fCounter++) {
Path path = Paths.get(MessageFormat.format(PATH_FMT, userHome, now, fCounter));
try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)) {
this.workBook.write(out);
this.workBook.close();
return path.toFile();
} catch (FileAlreadyExistsException incrCounterAndRetry) {
}
}
}
here an example on how i create the pivot tables (also working fine):
private void createPivotSid(XSSFSheet sheet) {
XSSFSheet data = workBook.getSheet("SID Table");
CellReference cr = new CellReference("A1");
CellReference c1 = new CellReference(0, 0);
CellReference c2 = new CellReference(data.getPhysicalNumberOfRows() - 1, data.getRow(0).getLastCellNum() - 1);
AreaReference ar = new AreaReference(c1, c2);
XSSFPivotTable pivotTable = sheet.createPivotTable(ar, cr, data);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).setAxis(STAxis.AXIS_COL);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).addNewItems();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).getItems().addNewItem()
.setT(STItemType.DEFAULT);
pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(5);
pivotTable.addRowLabel(11);
pivotTable.addRowLabel(12);
pivotTable.addRowLabel(2);
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);
}
This is all working fine but i get this annoying popup. So I googled much and found this question on SO and tried what they suggested. The answer with simply evaluating all Formulas does not work (no formulas here). So i tried the answer which suggests editing the xml-File before opening. Here i run into an problem when creating/setting defined names (not sure what this is, copied from example xml). My code is:
private void setWorkbookXmlContent() {
workBook.getCTWorkbook().addNewFileVersion();
workBook.getCTWorkbook().getFileVersion().setAppName("xl");
workBook.getCTWorkbook().getFileVersion().setLastEdited("5");
workBook.getCTWorkbook().getFileVersion().setLowestEdited("5");
workBook.getCTWorkbook().getFileVersion().setRupBuild("9303");
workBook.getCTWorkbook().getWorkbookPr().unsetDate1904();
workBook.getCTWorkbook().getWorkbookPr().setDefaultThemeVersion(124226);
workBook.getCTWorkbook().getBookViews().getWorkbookViewArray(0).setXWindow(480);
workBook.getCTWorkbook().getBookViews().getWorkbookViewArray(0).setYWindow(105);
workBook.getCTWorkbook().getBookViews().getWorkbookViewArray(0).setWindowWidth(27795);
workBook.getCTWorkbook().getBookViews().getWorkbookViewArray(0).setWindowHeight(14310);
workBook.getCTWorkbook().getBookViews().getWorkbookViewArray(0).unsetActiveTab();
workBook.getCTWorkbook().addNewDefinedNames().addNewDefinedName().setName("_xlnm._FilterDatabase");
workBook.getCTWorkbook().addNewDefinedNames().addNewDefinedName().setLocalSheetId(3);
workBook.getCTWorkbook().addNewDefinedNames().addNewDefinedName().setHidden(false);
workBook.getCTWorkbook().addNewDefinedNames().addNewDefinedName().setStringValue("SID Q3 2016'!$A$1:$M$81");
workBook.getCTWorkbook().addNewCalcPr().setCalcId(145621);
}
The xml-File i am trying to achieve looks like this (from Excel file of my coworker):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/>
<workbookPr defaultThemeVersion="124226"/>
<bookViews><workbookView xWindow="480" yWindow="45" windowWidth="27795" windowHeight="14370"/></bookViews>
<sheets><sheet name="Overview SID" sheetId="3" r:id="rId1"/><sheet name="Overview Exchange" sheetId="5" r:id="rId2"/><sheet name="Overview Total Cost" sheetId="6" r:id="rId3"/><sheet name="SID Q3 2016" sheetId="1" r:id="rId4"/><sheet name="Exchange Q3 2016" sheetId="2" r:id="rId5"/><sheet name="Total Cost Q3 2016" sheetId="4" r:id="rId6"/></sheets>
<definedNames><definedName name="_xlnm._FilterDatabase" localSheetId="3" hidden="1">'SID Q3 2016'!$A$1:$M$81</definedName></definedNames>
<calcPr calcId="145621"/>
<pivotCaches><pivotCache cacheId="8" r:id="rId7"/><pivotCache cacheId="15" r:id="rId8"/><pivotCache cacheId="21" r:id="rId9"/></pivotCaches>
</workbook>
But what i actually get is this:
<?xml version="1.0" encoding="UTF-8"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/><workbookPr defaultThemeVersion="124226"/>
<bookViews><workbookView xWindow="480" yWindow="105" windowWidth="27795" windowHeight="14310"/></bookViews>
<sheets><sheet name="SID Table" r:id="rId3" sheetId="1"/><sheet name="Exchange Table" r:id="rId4" sheetId="2"/><sheet name="Total Cost Table" r:id="rId5" sheetId="3"/><sheet name="Overview SID" r:id="rId6" sheetId="4"/><sheet name="Overview Exchange" r:id="rId8" sheetId="5"/><sheet name="Overview Total Cost" r:id="rId10" sheetId="6"/></sheets>
<definedNames><definedName localSheetId="3"/></definedNames>
<definedNames><definedName hidden="1"/></definedNames>
<definedNames><definedName>SID Q3 2016'!$A$1:$M$81</definedName></definedNames>
<definedNames><definedName localSheetId="3"/></definedNames>
<calcPr calcId="145621"/>
<pivotCaches><pivotCache cacheId="2" r:id="rId7"/><pivotCache cacheId="3" r:id="rId9"/><pivotCache cacheId="4" r:id="rId11"/></pivotCaches>
</workbook>
Maybe someone has a solution to my problem or can me help with the xml-editing. Any help is appreciated.
UPDATE
I found a way to get rid of the popup (pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setRefreshOnLoad(false);
) but now I need to "evaluate" all the pivot tables before saving. Or is it possible to save the workbook, then open the file in a new workbook (this should refresh the pivot tables?) and then save that?