views:

581

answers:

4

I build a very large POI workbook, on my web server. Holding the entire workbook in memory , will not scale for multiple concurrent requests. Is there a way i can progressively write the workbook to the servlet output stream. That should reduce the response time , as well as make the process memory efficient.

A: 

Unfortunately, that's impossible when there's no means of sequential data. I'd suggest to look for another format, e.g. CSV or XML. Both can be written out sequentially. If it's coming from a DB, it can even be done more efficient since a decent DB has builtin facilities to efficiently export to those formats. You just have to stream the bytes from one to other side.

BalusC
A: 

If you use JExcel It has sample code to read stream code to and from a Servlet. http://jexcelapi.sourceforge.net/resources/faq/

The only downside to this API looks like it only supports up to Excel 2003 inclusive.

Using POI - Can you not create the file and serve the file's bytes to the servlet output stream ?

Romain Hippeau
I am writing the POI workbook to the servlet outputstream. What happens behind the scenes, is it writes thte bytes into the outputstream. My question is, I don't know how to write it progressively. I have to wait until the entire workbook is created, and only then write it to I/O. And it takes around a minute to write to I/O. Don't know if that is justified.
The Machine
How big is your spreadsheet ?Do you create the spreadsheet, or is it given to you ?If it given to you, do you modify it ?What is taking so much time to write to I/O ?
Romain Hippeau
+1  A: 

If you are about to generate Excel 2007 (xslx) then you could adapt the approach of BigGridDemo.java as described here: http://www.realdevelopers.com/blog/code/excel

The solution is to let POI generate a container xslx as a template only and stream the actual spreadsheet data as XML into a zip output stream. Streamlining the XML generation is then up to you.

hlg
A: 

Did you tried with the write method direct to the HttpServletResponse.getOutputStream()?

Please take a look at the following example:

 HSSFWorkbook wb = new HSSFWorkbook();
 HSSFSheet sheet = wb.createSheet("new sheet");
 ...
 OutputStream out = response.getOutputStream();
 wb.write(out);
 out.close();
mtrovo
-1 This does not answer the question. The OP explicitly states that the issue he is trying to solve is how to avoid creating everything and then writing it out all at once.
Ophidian