Create Chart in Excel - Java POI Example Program - Part 3

We are trying to create a Pie Chart in Excel, using POI and JFreeChart. In the last part, we were successful in creating a chart and writing it to the output stream. Now, we have to convert the output stream to input stream and write the chart back to the Excel document. In this part, we will see how to do this and we will head to the final part of this series, that will provide the complete Java Program and the successful output.

Convert OutputStream (chart data) to Input Stream 

We have the chart in a ByteArrayOutputStream object. We can use toByteArray() method in this object and read the data back into InputStream. This can be processed in POI and written back into the same worksheet. Once we read from the output stream, we can get a unique ID for the chart by using workbook.addPicture method. (we have discussed this in our picture adding basics tutorial)
                /* We now read from the output stream and frame the input chart data */
                InputStream feed_chart_to_excel=new ByteArrayInputStream(chart_out.toByteArray());
                byte[] bytes = IOUtils.toByteArray(feed_chart_to_excel);
                /* Add picture to workbook */
                int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);

Create Chart Patriarch / Set Anchor Points:

In this step, we create the drawing Patriarch that will hold the chart in the worksheet. We also define anchor points, that will define where the chart will be positioned relative to the worksheet.

                /* Create the drawing container */
                HSSFPatriarch drawing = my_sheet.createDrawingPatriarch();
                /* Create an anchor point */
                ClientAnchor my_anchor = new HSSFClientAnchor();
                /* Define top left corner, and we can resize picture suitable from there */

Add Chart as a Picture to the Worksheet

In this step, we call the createPicture method and stamp the chart back to the worksheet. Following this, we resize the chart relative to the anchor points.

                /* Invoke createPicture and pass the anchor point and ID */
                HSSFPicture  my_picture = drawing.createPicture(my_anchor, my_picture_id);
                /* Call resize method, which resizes the image */

Almost there. We have the chart now in our logical workbook object! The last and final step is to write the data back on the disk and we are done.

Write Pie Chart XLS to Disk

Here, we write the workbook with chart, back to the disk. This is shown below:

                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("my_chart.xls"));

We have completed the step by step guide. We can now head to the final part of this tutorial series, where we will provide the full Java program for this example series and the output. Head to the final part by clicking on the links below.
This Tutorial: How to Add a Chart to Excel Document in Java with POI?
Keywords: Apache POI Chart Example, JFreeChart, Create Chart in Excel Using Java, Create Chart in POI  Example.
  1. Adding Charts in Apache POI - Introduction
  2. Step by Step Guide to Creating Charts
  3. Step by Step Guide to Creating Charts - Contd
  4. Full Java Program Example with Output

No comments:

Post a Comment