No need to write it to a file, use something like:
OutputStream sos = new DeflaterOutputStream(respo
Main Topics
Browse All TopicsHi all,
I'm using the Java Excel API from http://www.andykhan.com/je
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
response.setHeader( "Content-Disposition", "attachment; filename=Product-Export.xl
ServletOutputStream sos = response.getOutputStream()
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("lati
WritableWorkbook workbook = Workbook.createWorkbook(so
WritableSheet sheet = workbook.createSheet("Shee
for (int i=0; i<fields.length; i++) {
Label label = new Label(i, 0, (String)columnNames.get(fi
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();
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If it is possible using objects way then it would be best,
and you can make convert it to ZipOutputStream to send it in Zip format
http://javaalmanac.com/egs
The suggestions are good. Except the Java Excel API you use doesn't work with java.util.zip.ZipOutputStr
As a workaround following class can be used:
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipOutputStr
public class NoFlushZipOutputStream extends ZipOutputStream {
public NoFlushZipOutputStream(Out
super(out);
}
public void flush() throws IOException {}
}
And here are the changes to the servlet code:
response.setContentType("a
response.setHeader( "Content-Disposition", "attachment; filename=Product-Export.zi
NoFlushZipOutputStream sos = new NoFlushZipOutputStream(res
ZipEntry ze = new ZipEntry("Product-Export.x
sos.putNextEntry(ze);
WorkbookSettings settings = new WorkbookSettings();
[...]
workbook.write();
workbook.close();
sos.close();
Thanks all for the suggestions :)
mzhoreli: Your solution worked perfectly! Thanks very much!
Since you seem familiar with this API I'm wondering what you thoughts are of it. I've used it for awhile now and love it because I find it easy to use and it's handled everything I've needed it to do up until now. I remember I first chose to use it because I found POI (I think that's what it was called) confusing. Now that I'm at a point where I need to write huge excel files quite often I wonder if I need to consider using another package. Any thoughts?
Thanks again,
-Pat
Business Accounts
Answer for Membership
by: CEHJPosted on 2004-08-28 at 03:16:13ID: 11920397
>>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.
You wouldn't be able to if you were compressing it. Not setting the content length is not unusual though. Any online Javadoc for this API?