Search code examples
javaapache-poiexport-to-excel

Exporting a dynamically generated table to excel


I have a Java application where reports are generated dynamically in the form of tables. I need to have a button on a webpage whereby clicking on it will export the table to Excel. The table has to be saved by the user at the client side. How can I achieve this functionality? Will Apache POI API be helpful?


Solution

  • I used POI successfully in the past, but it has its quirks. The best way I've used it is to have a "template" file with all your formatting, colouring, etc. in your server, and copy and populate it with your data when exporting.

    But if your data is a simple column-separated table, and you don't worry yoo much about formatting and data types, why not exporting the data in plain text? (CSV format) This can be opened by Excel, and will be far more efficient in term of speed an memory usage when exporting your data.

    It will also bit a lot simpler to generate, you can use your own POJO generator, which will be a lot easier to test.

    Use POI only if your users expect dedicated date format or distinction between Integer / Text cells. Or if you need dynamically calculated cells (using formulas) If not, you can export everything as Strings, and Excel will find its way when opening the csv file.