It will work.

The IllegalStateException is because you have written something out to the
JspWriter (out in a JSP page), then writing binary data to
response.getOutputStream().

In the Servlet API, you can do either one or the other, but not both.

Most likely, you are actually trying to write to the JspWriter (out) *after*
you have sent the workbook to response.getOutputStream(). This is most
likely white space that occurs after your closing '%>' but before the end of
file.

<%
       HSSFWorkbook wb =  new HSSFWorkbook();
       HSSFSheet s = wb.createSheet();
       response.setContentType("application/vnd.ms-excel");
       response.setHeader("Content-Disposition","attachment; filename
=unknown.xls");
       OutputStream out = response.getOutputStream();
       wb.write(out);
       out.close();
%>  **This line is the same as 'out.println();'

By the way, the problem happens at the beginning of the JSP page as well.
For example, most JSP pages begin with

<%@ page language="java" %> ** this end-of-line is the same as
'out.println();'
<%@ page import="java.util.*" %> ** this end-of-line puts another
'out.println();'
<%@ page import="org.apache.poi.hssf.usermodel.*" %> ** and another...
<%
       HSSFWorkbook wb =  new HSSFWorkbook();
       HSSFSheet s = wb.createSheet();

This is why, if you look at a JSP page through Internet Explorer, then use
IE's View | Source menu command, you see a lot of blank lines at the top of
the page.

However, the good news is, if you write only a small amount to the character
stream (JSP 'out'), you are allowed to start over on the binary stream
('response.getOutputStream()'). Exactly how much you are allowed to send to
the character output stream without an error depends on your application
server. But it seems to be like 2K in most cases.

The point is that JSP wants to write the response to its character output
stream. But to send a binary workbook in the response, you need to use the
binary output stream that is returned by 'response.getOutputStream()'. So
you have to make sure that the character output stream does not get mixed
in. But as I have already explained, JSP will automatically send a
End-Of-Line to the character stream after each closing %>

The solution I have found is to put a 'return;' after the workbook is
complete.  I also indent my <% and %> kind of weird. Not really necessary,
but weird matches my personality :). When you put your JSP tags this way,
there is no extra white space sent to the character stream.

 <%@
       page language="java"
 %><%@
       page import="java.util.*"
%><%@
       page import="org.apache.poi.hssf.usermodel.*"
%><%
       HSSFWorkbook wb =  new HSSFWorkbook();
       HSSFSheet s = wb.createSheet();
       response.setContentType("application/vnd.ms-excel");
       response.setHeader("Content-Disposition","attachment; filename
=unknown.xls");
       OutputStream out = response.getOutputStream();
       wb.write(out);
       out.close();
       return;
%>

Try the above -- add a return after your out.close() - and let us know how
well it works

-----Original Message-----
From: Srilatha [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 30, 2003 3:14 AM
To: [EMAIL PROTECTED]
Subject: jsp data into Excel

Hi,
I want to export the contents of html page(JSP)  to Excel.The jsp  page
will be displayed on the browser and on the click of a button I need to
export the data into excel.On my page I have charts and also normal tables.
I have tried doing this but I'm getting illegalStateException..may be
bcoz its already written the jsp page and I'm trying to write the same
thing again ...
First of all is it possible to write the  contenets of this html page
into an Excel sheet???
Can u plz help

The code whcih I have written is like this....

       HSSFWorkbook wb =  new HSSFWorkbook();
       HSSFSheet s = wb.createSheet();
       response.setContentType("application/vnd.ms-excel");
       response.setHeader("Content-Disposition","attachment; filename
=unknown.xls");
       OutputStream out = response.getOutputStream();
       wb.write(out);
       out.close();

Thanks.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to