Jsp And Excel (Wiki forum at Coderanch) (original) (raw)

How can I ... Excel ... from JSP?

Excel is a commercial product - owned, distributed, and maintained by Microsoft Corporation. It is not an open standard.
There is nothing inherent in JSP that allows you to work directly with Excel (or any MS-Office) files.

That being said, if you wish to have your JSP results displayed in an instance of Excel on the user's desktop or from within their browser, there are two routes you can take.

Excel can interpret several text formats.
The two that are easiest to produce from a JSP page are "comma separated values" (CSV) pages or HTML tables.

You can request that the browser open the results in Excel and even suggest a filename to use when the user saves the file by setting the following two response headers:
Content-Type
Content-Disposition

The response object provides a convenience method for setting the Content-Type header. Both can be set with the setHeader method.

(For the sake of backward compatibility, we'll use the older and uglier "scriptlet" syntax)

[code=java]<%-- Set the content type header with the JSP directive --%>

This will suggest that the browser open up a full instance of Excel.
If you change the word "attachment" to "inline" within the Content-Disposition header,
the browser will open an embedded instance of Excel.

NOTE: If the user doesn't have Excel installed on their machine or if the browser doesn't
have "application/vnd.ms-excel" listed in it's mime types, the browser will merely prompt
the user to either save the file or two select an application for opening it.


JspFaq