I have a Java based web app that downloads data into an excel file. I'm using Apache poi 4 to prepare workbook (org.apache.poi.xssf.streaming.SXSSFWorkbook
). I checked the write method of workbook class, it does write data into an output stream. However, that happens after the complete data is written in the workbook and it takes time because of huge amount of data. I'm executing following steps:
- Execute query
- Create workbook
- Iterate over the query result set and create rows and cells with data. (I am looking for a solution to write this thing straight to stream)
- Write data into stream.
write(OutputStream stream)
Apache org.apache.commons.csv.CSVPrinter
does write data straight into the stream.
try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(outputStream), CSVFormat.DEFAULT)) {
printer.printRecord("id", "userName", "firstName", "lastName", "birthday");
printer.printRecord(1, "username1", "UserOne", "LastOne", LocalDate.of(1973, 9, 15));
printer.println();
printer.printRecord(2, "username2", "UserTwo", "LastTwo", LocalDate.of(1985, 3, 29));
} catch (IOException ex) {
ex.printStackTrace();
}
This is exactly what I need. Please suggest if I can achieve this using apache-poi or any other framework.