Bug 48042

Summary: Cannot save changes made to a chart
Product: POI Reporter: Kalpana <nkalpana>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED INVALID    
Severity: critical    
Priority: P2    
Version: 3.5-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: Original file containing a simple barchart
The file created after Chartspace doc changes. Excel throws "unreadable content" error when this is opened, and removes the drawing1.xml part

Description Kalpana 2009-10-22 20:20:43 UTC
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
Comment 1 Kalpana 2009-10-22 20:22:37 UTC
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
Comment 2 Yegor Kozlov 2009-10-28 06:00:03 UTC
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
Comment 3 Kalpana 2009-10-28 07:17:55 UTC
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