This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Servlets and the fly likes Export to excel question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Export to excel question" Watch "Export to excel question" New topic
Author

Export to excel question

Nagaraju Nookala
Greenhorn

Joined: Mar 19, 2004
Posts: 23
Hi,

I've jsp file that will extract data from the database and export it to excel file. Browser will have a popup that prompt the user with a "Save As" prompt.

The problem I'm having is that for the file name, the browser is using the name of the servlet (say "Copy of Example.do" as the file name).

So my question is how do I get the correct file name to show up. I'm trying to get this to work for the IE 6 browsers.

The headers are being set as follows:

response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "inline; filename=\""+fileName+"\"");

Any help would be appreciated.

Thanks,
Nookala
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Moved to the Servlets forum as this is not a JSP issue.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Try this before setting the content type


instead of this

Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 805

and if you wish to do more complex things look at the Apache POI Project, in particular the HSSF Page


Regards Pete
Nagaraju Nookala
Greenhorn

Joined: Mar 19, 2004
Posts: 23
Thanks for the reply. If I use,
response.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
I can open the file in a new window but not in the current window. The file opened in the new window will have the name with a (1) at the end as the filename (say filename(1)). Is there a way to fix this? When I download the file and hit "save" button on the "open-save-cancel" dialog it saves the file with correct name. If I hit "open" button and use File>Save As... I am having the problem.

Thanks,
Nookala.
Harsha Pherwani
Greenhorn

Joined: Jul 11, 2005
Posts: 27
Has anyone resolved this issue of the naming convention? I am facing a similar situation now.

Here is my code in JSP:
<%@ page contentType="application/vnd.ms-excel" %>
<% response.setHeader("Content-Disposition", "attachment; filename=\"abc.xls\""); %>

When this code is executed, an open save dialog box opens up. If I hit save, it saves my file correctly as "abc.xls" BUT if I hit open, it opens the excel file with name "abc[1].xls" and the sheet name is ".xls]abc[1]".
Due to this, when user hits save on the excel sheet, it fails saying there shouldn't be any special characters like "[", "]". User has to now rename the file to save it (which I dont want).

This issue is only with IE. I'm using IE6 version currently. Firefox opens and saves it correctly.

If anyone can throw some light as to what I should do to resolve this and have the file name be correct as specified with the trailing numbers ans brackets and not have the user to rename the file and then save, it'll be great!

Thanks in advance.

Harsha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Export to excel question