Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create excel chart using Apache POI

I need to create excel sheet from my Java code which contains charts like Bar chart, Line Chart etc using the Apache POI library. Is it possible? I am not able to find any useful code example for the same.

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

like image 701
javdev Avatar asked Jul 28 '10 14:07

javdev


People also ask

Does Apache POI support XLS?

Apache POI is able to handle both XLS and XLSX formats of spreadsheets. Some important points about Apache POI API are: Apache POI contains HSSF implementation for Excel '97(-2007) file format i.e XLS. Apache POI XSSF implementation should be used for Excel 2007 OOXML (.


4 Answers

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.

like image 111
liya Avatar answered Sep 28 '22 01:09

liya


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

See this discussion.

And in particular, the example.

like image 34
prule Avatar answered Sep 27 '22 23:09

prule


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.

like image 22
Adrian A. Avatar answered Sep 27 '22 23:09

Adrian A.


The thread looks old and I do not know if you have already figured out a way or are still looking for one.

But here is what I would do. There is a free library called JFreeChart. You can use that to generate either a JPG or PNG file which you can then insert into excel file when you create it with Apache POI.

But the disadvantage with this method is that the data in the graph will not change dynamically when you change the data in the spreadsheet as is the case with Excel. Now that is something I cannot live with.

So I'm going to do some research now. I'm pretty sure that since the question has been asked there must be an addition of a feature in Apache POI or another elegant way of doing things. If I find any I will be sure to post my findings here.

---- UPDATE ----

In my research lasting for about an hour, I could only find one suitable library called SmartXLS (please google it, I could not post the link because i'm a newbie and the spam prevention mechanism kicked in) that is remotely close to what I would use. You can generate both the excel and the chart via a program. The website is very simple and I could not find any licensing information so I'm assuming it is free for personal and commercial use. I was able to download the library without any problem. I have not used it yet. Give it a shot and let us know how it works out.

like image 38
Karthic Raghupathi Avatar answered Sep 28 '22 00:09

Karthic Raghupathi