I have an requirement of generating two excel files at once. We are using Apache POI for generating excel files and struts framework.
Within the action class, the steps are like,
OutputStream outputStream = response.getOutputStream();
and then populate data from db and created one file using POI and called,
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=ExportFile1.xls");
workbook.write(outputStream);
outputStream.flush();
and followed same steps(populate data from db, write excel content) for second file as,
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=ExportFile2.xls");
workbook.write(outputStream);
outputStream.flush();
But only one file is generated.
Is this possible in java to generate two files at once? Have anyone implemented this?
Pls note: Generating two sheets within one excel file is not required.
I had similar requirements, but this wasn’t to “generate with one click”, because if the user would be presented with “save as” dialogs for two or more files would be very confusing to the end-user.
So there was a “generate” button, that would generate the files (with a progress bar if the procedure takes more time) and than redirect to another page (or popup) that would contain individual links of those multiple files, so the user could take them one by one, or only the desired ones.
If the user still needs to get “with only one click” than zipping all files is the standard procedure. For more usability, that could be combined/improved with the screen or popup from scenario #1 : e.g. to show a list with checkboxes for each file, and a link at the bottom to “get all as a zip”. That way, the user could download individually files, but also select only the files is interested in, or simply just get all.
This is how most webapps do it since it’s the most user friendly.