Two Laptop Bag*
The moose likes Servlets and the fly likes Export to Excel not working with Internet Explorer HSSF and POI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Export to Excel not working with Internet Explorer HSSF and POI

James Blarney
Greenhorn

Joined: Oct 02, 2009
Posts: 10
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:

from Apache POI FAQ: http://poi.apache.org/faq.html

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.

The POI mailing list says that I have to do this: http://www.mail-archive.com/poi-user@jakarta.apache.org/msg08720.html

Hi Dragen,

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 ...
Regards,
Dave Fisher



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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Please do not post the same question more than once. Continue any discussion to your topic in the Open Source Project forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: Export to Excel not working with Internet Explorer HSSF and POI
 
Similar Threads
download zipped excel help!!!
Writing on excel file using POI
Exporting some data to excel
Export to Excel via a servlet with POI not working under internet explorer 6
NullPointerException