File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes facing problem in writing Excel Doc bytes into ServletOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "facing problem in writing Excel Doc bytes into ServletOutputStream" Watch "facing problem in writing Excel Doc bytes into ServletOutputStream" New topic
Author

facing problem in writing Excel Doc bytes into ServletOutputStream

Karthik Saravanan
Greenhorn

Joined: May 17, 2008
Posts: 2
Hi,

I am facing a problem in downloading the Excel Document in Browser either as an Attchement or inline type. First, the Browse asks me whether the Excel Document to be saved or opened. After I chose either save or open, it is opening the MS-Excel Application well, but before displaying workbok, there is an error message box saying that "Microsoft Excel for Windows has encountered a problem and needs to close. We are sorry for the inconvenience........ ".

I have written my code as below:

1) using Apache POI 3.0.2, I have workbook with a worksheet,cells and its values.
2) getting the bytes from the Workbook instance.
3) reading the bytes by using the BytesArrayInputStream piped with BufferedInputStream
4)reading the bytes in bulk-by-bulk (ie., 2048) and writing the same into BytesArrayOutputStream
5) flushing out the data from BytesArrayOutputStream into ServletOutputStream


-------------My Code snippet from a Servlet as below......----------------

response.setHeader("Content-disposition","attachment; filename=" +ts+ ".xls");
response.setHeader("Pragma", "Public");
response.setContentType("application/vnd.ms-excel; name=�Excel�");
ByteArrayInputStream bais = new ByteArrayInputStream(output);
BufferedInputStream bis = new BufferedInputStream(bais);
ServletOutputStream sos = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
baos.write(buff, 0, bytesRead);
}
baos.writeTo(sos);
sos.flush();
baos.close();
return;
-------------My Code snippet from a Servlet ends here ----------------


Could anyone help, as this is very urgent for me.


thanks,
Karthik
Esteban Puertas
Greenhorn

Joined: Mar 23, 2008
Posts: 22
Hi,
Try using this code (it is almost the same thing, it was used in one of my projects):
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Hi,

try this code, it will solve your problem.

Don't Convert the workbook to bytes. instead use the method available in
workbook itself.

response.setContentType("application/vnd.ms-excel");
wb.write(out);

it is working in my application.
Karthik Saravanan
Greenhorn

Joined: May 17, 2008
Posts: 2
Hi Esteban,

Thanks for your efforts. Ths is for you info that I am not creating the workbook physically, but creating on-the-fly and sending it to the outputstream that is what is our reqmnt.

--------------------

Hi Thennam Pandian,

Thanks a lot for your answer. I tried it out and It is working fine. I am happy that my problem is sovled.
[ May 19, 2008: Message edited by: Karthik Saravanan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: facing problem in writing Excel Doc bytes into ServletOutputStream