Meaningless Drivel is fun!
The moose likes Servlets and the fly likes Write Excel file to browser (client) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Write Excel file to browser (client)" Watch "Write Excel file to browser (client)" New topic

Write Excel file to browser (client)

Pappu Kumar
Ranch Hand

Joined: Oct 01, 2002
Posts: 69
I have a servlet that i use to write a excel file and then send it back to user. So the user can save it
or open it in excel. The problem is how do i give a name to this excel file. Because right now, if the
user chooses to open the excel file (rather than saving it). It takes the name of the file same as the name of the servlet
that was used to create the file...ex : http://localhost:7001/app/

Here is the snippet of the servlet . I use POI to create excel file

HSSFWorkbook workBook = CreateExcel.writeMSExcel(list.toArray(), lst); // CreateExcel is my helper class
// Write the output
OutputStream out = response.getOutputStream();

Please help....
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Moving this to the Servlet forum

[How To Ask Questions On JavaRanch]
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
You have to set the header on the servlet response, like below:

response.setHeader("Content-Disposition", "attachment;filename="+fileName);

If you don't want to force the user to download the file, you can use with the inline option:

response.setHeader("Content-Disposition", "inline;filename="+fileName);

[ November 23, 2004: Message edited by: Julia Reynolds ]
I agree. Here's the link:
subject: Write Excel file to browser (client)
It's not a secret anymore!