Created attachment 24408 [details] Original file containing a simple barchart I tried saving a change made to a chart of an Excel 2007 file for(POIXMLDocumentPart poixmlpartSheet : xssfSheet.getRelations()) { if(poixmlpartSheet.getPackageRelationship().getRelationshipType().equalsIgnoreCase("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings") == false) { if(poixmlpartSheet.getPackageRelationship().getRelationshipType().equalsIgnoreCase("http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing") == true) { for(POIXMLDocumentPart poixmlpartDrawing : poixmlpartSheet.getRelations()) { if(poixmlpartDrawing.getPackageRelationship().getRelationshipType().equalsIgnoreCase("http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart") == true) { ChartSpaceDocument docChartSpace = ChartSpaceDocument.Factory.parse(poixmlpartDrawing.getPackagePart().getInputStream()); CTChartSpace xChartSpace = docChartSpace.getChartSpace(); CTChart xChart = xChartSpace.getChart(); CTTitle xTitle = xChart.addNewTitle(); xTitle.addNewTx().addNewRich().addNewP().addNewR().setT("new title"); xChart.setTitle(xTitle); XmlOptions oOptions = new XmlOptions(); docChartSpace.save(poixmlpartDrawing.getPackagePart().getOutputStream(), oOptions); } } } } } The original file "Barchart.xlsx" did not have a title. Is there a specific way to save changes to a chart? Regards, nk
Created attachment 24409 [details] The file created after Chartspace doc changes. Excel throws "unreadable content" error when this is opened, and removes the drawing1.xml part The file created after Chartspace doc changes. Excel throws "unreadable content" error when this is opened, and removes the drawing1.xml part
There are two errors: (1) PackagePart's output stream must be closed. (2) bodyPr is a required element in CT_TextBody, see 5.7.2.157 of the OOXML spec. The correct code to set chart title is as follows: ChartSpaceDocument docChartSpace = ChartSpaceDocument.Factory.parse(poixmlpartDrawing.getPackagePart().getInputStream()); CTChartSpace xChartSpace = docChartSpace.getChartSpace(); CTChart xChart = xChartSpace.getChart(); CTTitle xTitle = xChart.addNewTitle(); CTTextBody ctBody = xTitle.addNewTx().addNewRich(); ctBody.addNewBodyPr(); //required ctBody.addNewP().addNewR().setT("new title"); xChart.setTitle(xTitle); XmlOptions oOptions = new XmlOptions(); PackagePart part = poixmlpartDrawing.getPackagePart(); OutputStream out = part.getOutputStream(); docChartSpace.save(out, oOptions); out.close(); //must close Yegor
Thanks Yegor, I figured it out myself over the weekend. Reading up on the mandatory nodes required for chart title, helped me. I forgot to update this message, and close it. Sorry for the unnecessary thread. Regards, Kalpana