File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Export to excel question" Watch "Export to excel question" New topic

Export to excel question

Nagaraju Nookala

Joined: Mar 19, 2004
Posts: 23

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" 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.addHeader("Content-Disposition", "inline; filename=\""+fileName+"\"");

Any help would be appreciated.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

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

[Asking smart questions] [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: 820

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

Regards Pete
Nagaraju Nookala

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.

Harsha Pherwani

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/" %>
<% 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.

I agree. Here's the link:
subject: Export to excel question
It's not a secret anymore!