Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

opening the excel sheet from servlet

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sumit,

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


Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! I resolved this issue!
 
prriya pratheep
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you writing anything to the servlet's output stream?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic