You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Kalpana <nk...@gmail.com> on 2009/10/23 05:45:08 UTC

Adding a chart using POI

Hi,

I tried manually adding a chart part to a document, by creating the package 
part for the chart and drawing, and manually setting up the XML content.


InputStream streamInput = new FileInputStream("c:\\BarChart.xlsx");
            Workbook wb = WorkbookFactory.create(streamInput);
            XSSFWorkbook xssfWorkbook = (XSSFWorkbook)wb;
            CTWorkbook xWorkbook = xssfWorkbook.getCTWorkbook();
            XSSFSheet xssfSheet = xssWorkbook.getSheetAt(i);
 XSSFDrawing xssfDrawing = xssfSheet.createDrawingPatriarch();
 CTTwoCellAnchor xTwoCellAnchor = xssfDrawing.getCTDrawing
().addNewTwoCellAnchor();
            
....Added all the required child elements needed for a drawing part.
            xTwoCellAnchor.addNewClientData();

ChartSpaceDocument docChartSpace = ChartSpaceDocument.Factory.newInstance();
....Added all required child elements for a chartspace for a simple barchart
docChartSpace.setChartSpace(xChartSpace);


  PackagePartName pkgprtnameChart = PackagingURIHelper.createPartName(new URI
("/chart1.xml"));
            
            xssfDrawing.getPackagePart().addRelationship(pkgprtnameChart, 
TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/rel
ationships/chart", "something");
            PackagePart pkgprtChart = xssfSheet.getPackagePart().getPackage
().createPart(pkgprtnameChart, "application/vnd.openxmlformats-
officedocument.drawingml.chart+xml");
            StreamHelper.saveXmlInStream(DocumentHelper.parseText
(docChartSpace.xmlText()), pkgprtChart.getOutputStream());

   FileOutputStream fosOutput = new FileOutputStream("c:\\NewChartTest1.xlsx");
            wb.write(fosOutput); fosOutput.flush();





When I open the NewChartTest1.xlsx, Excel crashes, and generates a crash 
report. When it reopens after the crash, the drawing part is removed.


I tried saving the contents of the chartspace to the package part in a 
slightly different way as follows

ByteArrayOutputStream bos = new ByteArrayOutputStream();
PackagePart pkgprtChart = xssfSheet.getPackagePart().getPackage().createPart
(pkgprtnameChart, "application/vnd.openxmlformats-
officedocument.drawingml.chart+xml", bos);

bos.write(docChartSpace.xmlText().getBytes());
bos.flush();

FileOutputStream fsOutput = new FileOutputStream("C:\\NewChartTest1.xlsx");
wb.write(fosOutput);


IN this case, Excel does not crash when I open the output file, instead I get 
the "Excel found unreadable content in the file", and when I get past it, it 
says it removed the drawing part.
When I examined the package contents, i see the XML in perfect condition.

Something is going wrong internally, or I am missing sometihng. Anyone else in 
the same situation/knows whats missing?


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Adding a chart using POI

Posted by Kalpana <nk...@gmail.com>.
Kalpana <nkalpana <at> gmail.com> writes:

> 
> Hi,
> 
> I tried manually adding a chart part to a document, by creating the package 
> part for the chart and drawing, and manually setting up the XML content.
> 
> IN this case, Excel does not crash when I open the output file, instead I 
get 
> the "Excel found unreadable content in the file", and when I get past it, it 
> says it removed the drawing part.
> When I examined the package contents, i see the XML in perfect condition.
> 
> Something is going wrong internally, or I am missing sometihng. Anyone else 
in 
> the same situation/knows whats missing?
> 



I figured out what the problem was.
I was missing some nodes like Body properties, ListStyle, under Rich().
Also paragraph properties, and setting the language of the text run is very 
important.

When I added these missing nodes to the CTTile object, it worked.

The xml structure is very important. 
Also, we have to save the 
ChartSpaceDocument.getChartSpace().save(OutputStream, xmlOptions);



If anyone needs more info on this write 
to me at natarak@rediffmail.com

Regards,
Kalpana




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org