Create excel chart using Apache POI Create excel chart using Apache POI apache apache

Create excel chart using Apache POI


You can only use Excel template to preserve chart with POI.It mean you create an Excel template file with chart in it and link the chart's datasource to one definedName,then you can load the template file from POI and using code to change the definedName.Current POI does not support to create Chart from scratch.


In poi-3.8 support for charts seems to be coming.

See this discussion.

And in particular, the example.


Is there any other alternative for this apart from POI library for Java?

There are a few other libraries too, but I'm not sure if the can write such a thing like a chart.

If you can use the newer Office versions (the ones that use the XML based format files), than you could use a different approach:

  • create a an Excel file with a newer version of Office, containing the charts you need, and some dummy data.
  • unzip that excel file and extract the XML files from inside. Of most interest will be the files xl\worksheets\sheet1.xml xl\worksheets\sheet2.xml or xl\worksheets\sheet3.xml (depending on what sheets were used)
  • Take a look at the file format (it's more complicated than using POI), but it shouldn't be that hard to identify the "dummy" data you entered before.
  • Use Velocity or FreeMarker to transform that xml file into a template (by replacing your dummy data with variables and macros what would produce the same result)
  • Write a small program (just a few lines) that takes you real data, merges with the template, and packs everything back in a zip, but puts the *.xlsx extension.

I know that the above steps look a little complicated, but if you don't have too complicated Excel files, it should be easier than it looks.