poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kalpana <nkalp...@gmail.com>
Subject Adding a chart using POI
Date Fri, 23 Oct 2009 03:45:08 GMT
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


Mime
View raw message