Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Excel data directly to OutputStream (limit memory consumption)

Tags:

java

excel

I’m looking for a – simple – solution to output big excel files. Input data comes from the database and I’d like to output the Excel file directly to disk to keep memory consumption as low as possible. I had a look to things like Apache POI or jxls but found no way to solve my problem.

And as additional information I need to generate .xls files for pre 2007 Excel, not the new .xlsx xml format. I also know I could generate CSV files but I’d prefer to generate plain Excel…

Any ideas ?

I realize my question isn't so clear, I really want to be able to write the excel file without having to keep the whole in memory...

like image 660
pgras Avatar asked Dec 29 '22 05:12

pgras


2 Answers

The only way to do this efficiently is to use character-based CSV or XML (XLSX) format, because they can be written to the output line by line so that you can per saldo have only one line at once in the memory all the time. The binary-based XLS format must first be populated completely in memory before it can be written to the output and this is of course memory hogging in case of large amount of records.

I would recommend using CSV for this as it may be more efficient than XML, plus you have the advantage that the any decent database server has export capabilities for that, so that you don't need to program/include anything new in Java. I don't know which DB you're using, but if it were for example MySQL, then you could have used LOAD DATA INFILE for this.

like image 192
BalusC Avatar answered May 23 '23 11:05

BalusC


No idea for generating a real XSL file. But you can directly write a HTML file, or a zip stream containing an OPenDocument spreadsheet (I guess MSExcel can read this later format)

like image 20
Pierre Avatar answered May 23 '23 13:05

Pierre