Create excel chart using Apache POI

前端 未结 6 910
小蘑菇
小蘑菇 2020-12-09 17:49

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 u

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 18:15

    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.

提交回复
热议问题