File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

facing problem in writing Excel Doc bytes into ServletOutputStream

 
Karthik Saravanan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try using this code (it is almost the same thing, it was used in one of my projects):
 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic