Create Pie Chart in Excel Using Java - POI JFreeChart - Example Program

Create Pie Chart in XLSX Using POI / JFreeChart - Introduction

In the last tutorial series, we saw how to use JFreeChart with Apache POI to generate a Pie Chart in XLS file, using Java. We provided only an XLS example in the last part and in this post, we will provide XLSX example. We have a sample XLSX document with Pie Chart data source. We will read the data from the data source, create the Pie Chart and add it back to the worksheet with POI. We will provide a fully commented version of the Java Program so that you can understand what each step does. Let us get started now.

XLSX - Create Pie Chart - Apache POI - JFreeChart - Java Example

The complete Java program that creates a Pie Chart using Apache POI / JFreeChart and writes it back to the XLSX worksheet is provided below:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities; 
import java.util.Iterator;
public class CreatePieChartExampleXLSX {  
        public static void main(String[] args) throws Exception{                
                /* Read Excel and the Chart Data */
                FileInputStream chart_file_input = new FileInputStream(new File("xlsx_pie_chart.xlsx"));
                /* Read chart data from XLSX Workbook */
                XSSFWorkbook my_workbook = new XSSFWorkbook(chart_file_input);
                /* Read worksheet that has pie chart input data information */
                XSSFSheet my_sheet = my_workbook.getSheetAt(0);
                /* Create JFreeChart object that will hold the Pie Chart Data */
                DefaultPieDataset my_pie_chart_data = new DefaultPieDataset();
                /* We have to get the input data into DefaultPieDataset object */
                /* So, we iterate over the rows and cells */
                /* Create an Iterator object */
                Iterator<Row> rowIterator = my_sheet.iterator(); 
                /* Loop through worksheet data and populate Pie Chart Dataset */
                String chart_label="a";
                Number chart_data=0;            
                while(rowIterator.hasNext()) {
                        //Read Rows from Excel document
                        Row row =;  
                        //Read cells in Rows and get chart data
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell =; 
                                        switch(cell.getCellType()) { 
                                        case Cell.CELL_TYPE_NUMERIC:
                                        case Cell.CELL_TYPE_STRING:
                /* Add data to the data set */          
                /* Create a logical chart object with the chart data collected */
                JFreeChart myPieChart=ChartFactory.createPieChart("Excel Pie Chart Java Example",my_pie_chart_data,true,true,false);
                /* Specify the height and width of the Pie Chart */
                int width=640; /* Width of the chart */
                int height=480; /* Height of the chart */
                float quality=1; /* Quality factor */
                /* We don't want to create an intermediate file. So, we create a byte array output stream 
                and byte array input stream
                And we pass the chart data directly to input stream through this */             
                /* Write chart as JPG to Output Stream */
                ByteArrayOutputStream chart_out = new ByteArrayOutputStream();          
                /* We now read from the output stream and frame the input chart data */
                /* We don't need InputStream, as it is required only to convert the output chart to byte array */
                /* We can directly use toByteArray() method to get the data in bytes */
                /* Add picture to workbook */
                int my_picture_id = my_workbook.addPicture(chart_out.toByteArray(), Workbook.PICTURE_TYPE_JPEG);                
                /* Close the output stream */
                /* Create the drawing container */
                XSSFDrawing drawing = my_sheet.createDrawingPatriarch();
                /* Create an anchor point */
                ClientAnchor my_anchor = new XSSFClientAnchor();
                /* Define top left corner, and we can resize picture suitable from there */
                /* Invoke createPicture and pass the anchor point and ID */
                XSSFPicture  my_picture = drawing.createPicture(my_anchor, my_picture_id);
                /* Call resize method, which resizes the image */
                /* Close the FileInputStream */
                /* Write Pie Chart back to the XLSX file */
                FileOutputStream out = new FileOutputStream(new File("xlsx_pie_chart.xlsx"));

Datasource Input- Pie Chart XLSX Output - Example

Sample input for Pie Chart and the Output obtained using POI / JFreeChart is provided below:

Create Pie Chart in XLSX Workbook - POI JFreeChart Example Output - Input / Chart Generated
Create Pie Chart in XLSX Workbook - POI JFreeChart Example Output - Input / Chart Generated

The XLSX example to create Pie Chart is straight forward and can be derived easily from the XLS example for Adding Pie Chart. You may want to refer to that tutorial to get a base understanding of how we used JFreeChart and POI together to create the chart. If you have any questions, you can post it in the comments section of this post.


  1. IN my excel i am trying to ad multiple charts using for loop. But somehow the first chart is getting deleted.