473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help needed in excel download by servlet..

Hi all,
Am trying to download an excel kept in the server by java servlet. On click od download button, download dialog box appears and on clicking on save, the file gets downloaded and saved successfully. Everything in this is perfect. But on click of open button i want my excel to be opened in separate excel instead of opening the excel as embedded in the browser itself. Any guidelines would help me a lot. here is the code.. Am passing the file's path which is to be downloaded from jsp and getting the path in this servlet.

Should i have to use jexcel for writing into excel separately and pop up on click of open?? If so, can anyone explain with an example??

Expand|Select|Wrap|Line Numbers
  1. public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  2. String filename = request.getParameter("fileName");
  3. String dataFileLocation = "C:\"
  4. File f = new File(dataFileLocation+"\\"+filename);
  5. response.setContentType("application/vnd.ms-excel");
  6. response.setHeader("Content-Disposition", "inline; filename="+ filename);
  7. response.setHeader("Pragma", "public");
  8. response.setHeader("Cache-Control", "no-store");
  9. response.addHeader("Cache-Control", "max-age=0");
  10. FileInputStream fin = null;
  11. try {
  12.     fin = new FileInputStream(f);
  13. } catch (FileNotFoundException e) { e.printStackTrace();}    
  14. int size = 1024;
  15. try {
  16. response.setContentLength(fin.available());
  17. byte[] buffer = new byte[size];
  18. ServletOutputStream os = null;
  19. os = response.getOutputStream();
  20. int length = 0;
  21. while ((length = fin.read(buffer)) != -1) {    
  22.      os.write(buffer, 0, length);
  23.  }    
  24. fin.close();
  25. os.flush();
  26. os.close();
  27. } catch (IOException e) {    e.printStackTrace();}
  28. }
  29.  
Feb 9 '10 #1
0 2596

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: SubbaRao Karanam | last post by:
When I click the Button in Applet , I want it open a Dialog asking for the excel to open/save etc... It doesnt happen why... Now it doesnt create the excel file Why.... My Applet code is...
5
by: Grant Collins | last post by:
Hi I am writing a web based application as part of a small project that I am undertaking using servlets beans and jsp. I already have one servlet - bean - jsp page working and I have written...
9
by: Russ Perry Jr | last post by:
I'm using "ID" and "Value" in the generic sense here... Let's say one page I had a <html:select> with a collection like this: <html:options collection="items" property="key"...
5
by: Patrick | last post by:
I am trying to download the html page at http://www.dreamteamfc.com/dtfc04/servlet/PostPlayerList?catidx=1 Using the code public class DownloadWebPage { public static void main (String...
2
by: D. Shane Fowlkes | last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple of years to calculate a file's Estimated Download Time based off of a solid 50kbs connection (dial up). This is for a...
1
by: Coleen | last post by:
To export data from a datagrid to an excel file. I am using VB.net/ASP.net v 2003, and not connecting to an SQL database. I have an existing datagrid that I want to get the data from and download...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
9
by: mjahabarsadiq | last post by:
Hi I have created a servlet that is to be started at the server startup. And I got it. In that I have created a object of another class and set it as a session attribute. What I am trying is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.