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.
Use a third party library such as Jakarta POI to create and manipulate an actual Excel file on your server and stream that file to your user. See their page for documentation. http://jakarta.apache.org/poi/. The AccessingFileFormats page lists more options for working with Excel files in Java. Also the DisplayTag library has tags that wrap the Jakarta POI libraries, providing a higher level abstraction for JSP developers.
Build your page in a text format that Excel can understand and request that the user's browser open the results with Excel. The following describes how to do this.
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