'Apache POI: Is it possible to use SXSSF without temporary files?
I'd like to use SXSSF
(Streaming Usermodel API) from Apache POI.
What I don't like is that it uses temporary files.
Question:
Is it possible in Apache POI to flush directly to the output stream without the use of temporary files?
Solution 1:[1]
No
In order to generate a valid Excel .xlsx
file, there are various bits of the file which need to agree with each other. These references, links, ids etc need to be updated by Apache POI when writing the file out
You therefore have two options:
- XSSF - No temporary files, everything very easy to work with, everything kept in memory
- SXSSF - Various restrictions, large parts streamed into temporary files, small bits in memory
If you don't want temp files, buy some more memory and use XSSF!
Also, don't forget that you can control where POI places the temporary files, if the default doesn't work well for you
Solution 2:[2]
You can also compress the file
wb.setCompressTempFiles(true);
It will make a zip file of the temp file. But dispose is recommended.
Solution 3:[3]
Yes we can stop the temp file generation during excel file generation. Either you have to zip the temp file or dispose the temp file. But dispose is recommended as the file will not be created again if you call the dispose method.
Code Snippet
SXSSFWorkbook wb = new
SXSSFWorkbook(100);
// write your code.
wb.dispose();
// This method will help to stop the temp file generation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gagravarr |
Solution 2 | Somanath Behera |
Solution 3 | Codemaker |