Bug 48042 - Cannot save changes made to a chart
Summary: Cannot save changes made to a chart
Status: RESOLVED INVALID
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-FINAL
Hardware: PC Windows XP
: P2 critical (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-22 20:20 UTC by Kalpana
Modified: 2009-10-28 07:17 UTC (History)
0 users



Attachments
Original file containing a simple barchart (9.49 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2009-10-22 20:20 UTC, Kalpana
Details
The file created after Chartspace doc changes. Excel throws "unreadable content" error when this is opened, and removes the drawing1.xml part (6.65 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2009-10-22 20:22 UTC, Kalpana
Details

Note You need to log in before you can comment on or make changes to this bug.
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