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

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
....Added all the required child elements needed for a drawing part.

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

  PackagePartName pkgprtnameChart = PackagingURIHelper.createPartName(new URI
TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/rel
ationships/chart", "something");
            PackagePart pkgprtChart = xssfSheet.getPackagePart().getPackage
().createPart(pkgprtnameChart, "application/vnd.openxmlformats-
(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);


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

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

View raw message