This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

opening the excel sheet from servlet

 
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

 
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
 
Author
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
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!
 
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
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you writing anything to the servlet's output stream?
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome 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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!