Hi,

First of all sorry for the cross-posting but I really don't know which is
the correct list for my problem.

I use Struts and velocity to generate my HTML pages. I use POI to generate
some reports out of database data and other datasources such as CORBA
servers.

I face the following problem:

When using struts you usually create actions and forward any type of result
to a jsp or in my case to a velocity template. When I present a query result
as a HTML page, the user finds two links on that pages: 'get as CSV' and
'get as EXCEL'. I have written a servlet, which transforms data (that I keep
in a session object) into CSV or EXCEL using apache POI.

This looks as follows for Excel:

public class transformIntoExcel extends HttpServlet
{
    private static final String CONTENT_TYPE = "application/vnd.ms-excel";
    //Initialize global variables
    public void init() throws ServletException
    {
    }
    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
    {
        HttpSession session = request.getSession(false);
        response.setContentType(CONTENT_TYPE);
        //PrintWriter out = response.getWriter();

        ExcelSheet es = (ExcelSheet)session.getAttribute("ExcelSheet");
        if (es != null)
        {
            response.addHeader("content-disposition", "attachement;
filename="+es.getFileName());
            response.addHeader("content-disposition",
"filename="+es.getFileName());
            session.removeAttribute("ExcelSheet");
            ServletOutputStream servletOut = response.getOutputStream();
            es.getWorkbook().write(servletOut);
        }
    }
    //Clean up resources
    public void destroy()
    {
    }
}

I found out, that the interpretation of the header 'content-disposition'
seems to be absolutely browser dependant. I tried IE 5, 5.5 and 6, NS 6 and
Opera 6. The only correct behaviour I noticed with Opera. It opend a dialog
and asked me where to save the file. It showed a correct file name like
'MyExelSheet.xls' (Exactly what was returned by es.getFileName() in my code
above)

Some browsers (mostly microsoft IE) throw an error message saying that the
file could not be found. The file name is in most cases some crap like a
mixture of characters and numbers. Some browsers also display the servlet
name! I did a mapping of the above servlet to 'makeexcel' so some browsers
try to store a file 'makeexcel' without any extension.

I do something similar with CSV Files (comma separated values).
The content type is set there to
CONTENT_TYPE = "text/comma-separated-values"
This works much better and every browser in every version shows a file save
dialog and proposes the correct file name as I set it in my
content-disposition header.

I also found many different content types for excel such as
"application/vnd.ms-excel" or "application/x-msexcel" or "application/excel"

Does anybody know how this works? I like that the user MUST save the file
before opening. Some browsers as M$ IE use to open known file types directly
as OLE objects inside the browser unless the user configures the browser NOT
do do so (usually done for security reasons).  IE is a OLE container that
can open most OLE objects directly. I personally hate this behaviour because
it is highly insecure!!

Any help would be greatly appreciated.

Thomas


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

Reply via email to