The only way to do this efficiently is to use character-based CSV or XML (XLSX) format, because they can be written to output line by line so that you can only store one line at a time in memory all the time. First, the binary XLS format must be completely filled in memory before it can be written to the output, and this, of course, is a memory delay in the case of a large number of records.
I would recommend using CSV for this, as it can be more efficient than XML, plus you have the advantage that there is an export option on any decent database server, so you don’t need / enable anything new in java. I do not know which database you are using, but if it were, for example, MySQL, then you could use LOAD DATA INFILE for this.
source share