Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI output problem

I have a problem with Apache POI. I attempt to return a file after I have processed the relevant data. When I go to return the file to the browser(Both IE8/9, firefox), the browser returns a load of garbage characters. This only happens when the Excel file is large and the process has been running for say 2 minutes plus. Otherwise it returns a file which I can then open in Excel.

Any help is appreciated, thanks.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
OutputStream out = null;

try {
  out = new BufferedOutputStream(response.getOutputStream());
  wb.write(out);
  out.flush();     
} catch (IOException ex) {
  ex.printStackTrace();
}
like image 238
Clive Avatar asked May 06 '11 12:05

Clive


People also ask

Does POI use Log4j?

Since version 5.1. 0 Apache POI uses Apache Log4j v2 directly.

Can Apache POI be compiled used with Java 11?

23. Can Apache POI be compiled/used with Java 11? Including the existing binaries on Java 11 as normal jar-files should work when using recent versions of Apache POI.

Does Apache POI support multithreading?

Hi, We use excel file to read and write our test data (data driven framework) in Selenium. We use Apache POI to do this. But as you all know, Apache POI library does not support multithreading(especially when writing the data).

Why did u choose Apache POI over others to read data from Excel explain its advantages?

Apache POI provides excellent support for working with Microsoft Excel documents. Apache POI is able to handle both XLS and XLSX formats of spreadsheets. Some important points about Apache POI API are: Apache POI contains HSSF implementation for Excel '97(-2007) file format i.e XLS.


1 Answers

I think you should specify the content length as well. This is the line you should insert:

response.setContentLength(/* length of the byte[] */);

I suggest you using Apache Commons IOUtils class for dealing with byte arrays and streams.

like image 156
jabal Avatar answered Sep 19 '22 02:09

jabal