While working on a web application that generates Excel spreadsheets , I faced the issue mentioned below.
Sometimes the spreadsheet is opened up in a new MS-Excel application window and often opened in the same browser window. This is used to happen for Internet Explorer 6.0. On IE7 it was not reproducible i.e. used to open up in the MS-Excel application all the time leaving browser intact.
I had implemented this spreadsheet generation as below –
- Generate the spreadsheet using Apache POI.
- Set header content-type to “application/vnd.ms-excel“.
- In addition to content-type, I was also adding the header “Content-disposition” that assigns a filename to the content written on the response stream. Some excel files were named like “xyz/abc.xls“.
- Write the bytes on the Response stream.
We could figure out the behavior because when we ran the application and try to retrieve the report,
For spreadsheets with filenames that did not contain “/” displayed the browser File Download dialog properly with “filename.xls”.
Whereas if the filename contained “/” in it then the File Download dialog shows
- EITHER the URL that is used to retrieve the excel report from server. e.g. if you have a ReportServlet that serves the excel report then it would show “ReportServlet” instead of filename.
- OR some random filename as shown below
Once we fixed the filename generation logic to replace “/” with “-“(or any other valid character), reports started opening up in MS-Excel application than in same browser window.
Another thing to note here is that IE7 handles it internally by replacing “/” with “_” as shown below.
Note:In addition to this while searching I found out the Microsoft knowledge-base which mentioned this issue can be resolved by doing some client side settings. This knowledge-base can be found at KB-162059.