my dog learned polymorphism*
The moose likes Servlets and the fly likes opening the excel sheet from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "opening the excel sheet from servlet" Watch "opening the excel sheet from servlet" New topic
Author

opening the excel sheet from servlet

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi,

I have created a report on my local.I am trying to open this xls file inside my servlet:



This opens the file without some unknown name.

What am I doing wrong here?

Thanks

Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

The unknown name you are getting must be related to the URL pattern you are using for your servlet.
Use Java to break the filepath to get the file name and then pass the file name to setHeader method.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Thanks for the reply. I can now open the file with the name that I pass.However the file is empty.It does not have the data which the file created at the local drive has.Am I missing something?

The code is pasted below:
String filePath ="d:\\excel\\dbreport.xls";

res.setContentType("application/vnd.ms-excel");
PrintWriter output = res.getWriter();

res.setHeader("Content-Disposition", "attachment; filename=\"dbreport.xls\" ");

FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
output.close();

The file in the d drive location shows the correct data whereas the open which opens is empty.

Please advice

Thanks
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Moieen Khatri wrote:


String filePath ="d:\\excel\\dbreport.xls";

res.setContentType("application/vnd.ms-excel");
PrintWriter output = res.getWriter();

res.setHeader("Content-Disposition", "attachment; filename=\"dbreport.xls\" ");

FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
output.close();



Please tell what is "wb"?
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi Sumit,

wb is the the workbook object.Code is pasted below:


Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm confused: are you trying to write a file to the browser? You neither read a file nor write to the browser.
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
I am trying to write the contents of the workbook object to the xls file and trying to open the file:




Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Again--you never open a file for reading.

If wb.write(fileOut) writes the XLS file you still need to *open it* if you want to return the data to the browser. Or perhaps you could wb.write(...) directly to the response--depends on what you're doing.

But you never open a file for reading here, nor do you send anything to the browser.
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Thanks for the help! I resolved this issue!
prriya pratheep
Greenhorn

Joined: Jun 10, 2008
Posts: 22

Hi, I am facing the same problem. Opening a empty file .Please anybody help me to open it as mentioned by Moieen Khatri.Below is my code



Thanks and Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Are you writing anything to the servlet's output stream?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

As Rob asked, to get the excel content sent by Servlet, it should be written to Servlet OutputStream, then only it reaches the client(browser) to open the excel file.


SCJP 6 [SCJP - Old is Gold]
 
jQuery in Action, 2nd edition
 
subject: opening the excel sheet from servlet
 
Similar Threads
Save File on Client Machine
Using HSSFWorkbook to copy excel file
How to display pdf file in broswer
jsp with POI FILE System concept.
getting junk characters inside the excel sheet created from servlet