Export to Excel not working with Internet Explorer HSSF and POI
Joined: Oct 02, 2009
One of our customers is complaining about not being able to export to excel when using internet explorer. I did some research on the issue and it goes back to something called apache POI. POI claims that you have to change your code:
The problem usually manifests itself as the junk characters being shown on screen. The problem persists even though you have set the correct mime type.
The short answer is, don't depend on IE to display a binary file type properly if you stream it via a servlet. Every minor version of IE has different bugs on this issue.
The problem in most versions of IE is that it does not use the mime type on the HTTP response to determine the file type; rather it uses the file extension on the request. Thus you might want to add a .xls to your request string. For example http://yourserver.com/myServelet.xls?param1=xx. This is easily accomplished through URL mapping in any servlet container. Sometimes a request likehttp://yourserver.com/myServelet?param1=xx&dummy=file.xls is also known to work.
To guarantee opening the file properly in Excel from IE, write out your file to a temporary file under your web root from your servelet. Then send an http response to the browser to do a client side redirection to your temp file. (Note that using a server side redirect using RequestDispatcher will not be effective in this case)
Note also that when you request a document that is opened with an external handler, IE sometimes makes two requests to the webserver. So if your generating process is heavy, it makes sense to write out to a temporary file, so that multiple requests happen for a static file.
None of this is particular to Excel. The same problem arises when you try to generate any binary file dynamically to an IE client. For example, if you generate pdf files using FOP, you will come across many of the same issues.
We serve POI written XLS all the time through TOmcat 4.1.31 - we learned the hard way the following:
Do not serve through a JSP - write a Servlet:
(1) JSP is supposed to be TEXT only.
(2) In JSP it is easy to insert a space in your output before setting the header and that makes the output have mimetype=text/plain.
Then your url is /exportxls?parms=whatever
When you open your Excel file from the Web in IE - DO NOT let it open it in the Browser - Save it and Open. If anyones knows an easy ubiquitous way to unset that, I'd love to know.
If you use application/octet-stream and allow IE to open in the browser then you will start to have trouble with binary files that are served that way ...
How do I implement the advice above into this file: ExcelResult.java I already added those changes to the web.xml file.
This is my revised code. Keep in mind, im kind of a noob.
Ill be happy to provide more information as requested