Hi all,
I'm using the Java Excel API from
http://www.andykhan.com/jexcelapi/ and have a couple of questions. What I'm doing is creating an export to excel function that users can execute to retrieve large amounts of data through their browser with. It is currently working but I'm facing a couple of problems. First, this API only allows you to write the excel file in one go, which normally isn't a problem until you are exporting huge excel files. Some of my exports are ~20 MB's (and this is the way they want it so no arguing!) and with the default JVM setting it would crash with out of memory exceptions. I've increased the JVM's memory and tried to minimize memory usage elsewhere in the code to combat this problem.
What I'd like to do now is zip up the excel file before it's sent to the user but I'm having trouble figuring out how to compress the outputstream. Also, I would like to know how to set the size of the file in the response header but I can't figure out how to determine that with this API I'm using. I'm almost wondering if I should write the file to /temp on the server first, zip it up there, get the size info, and then send it to the user.
Here is the relevant part of the code. Any suggestions are greatly appreciated :)
response.setContentType("a
pplication
/octet-str
eam");
response.setHeader( "Content-Disposition", "attachment; filename=Product-Export.xl
s" );
ServletOutputStream sos = response.getOutputStream()
;
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("lati
n1");
WritableWorkbook workbook = Workbook.createWorkbook(so
s, settings);
WritableSheet sheet = workbook.createSheet("Shee
t 1", 0);
for (int i=0; i<fields.length; i++) {
Label label = new Label(i, 0, (String)columnNames.get(fi
elds[i]), headerFormat);
sheet.addCell(label);
}
int lineNumber = 1;
ListIterator iterator = products.listIterator();
while (iterator.hasNext()) {
Product thisProduct = (Product)iterator.next();
for (int i=0; i<fields.length; i++) {
createLabel(i, lineNumber, thisProduct, fields[i], sheet);
}
// Increment lineNumber each iteration and also get rid of the last product since we no longer need it.
// Make a suggestion to the JVM every 1000 rounds that it needs to do some housework :P
// (This has helped quite a bit with the memory problems).
if (lineNumber++ % 1000 == 0) System.gc();
iterator.remove();
}
workbook.write();
workbook.close();