Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 06:50 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
tester23 is on a distinguished road
Default Help needed in excel download...
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??


Code:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("fileName");
String dataFileLocation = "C:\"
File f = new File(dataFileLocation+"\\"+filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="+ filename);
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
FileInputStream fin = null;
try {
	fin = new FileInputStream(f);
} catch (FileNotFoundException e) { e.printStackTrace();}	
int size = 1024;
try {
response.setContentLength(fin.available());
byte[] buffer = new byte[size];
ServletOutputStream os = null;
os = response.getOutputStream();
int length = 0;
while ((length = fin.read(buffer)) != -1) {	
	 os.write(buffer, 0, length);
 }	
fin.close();
os.flush();
os.close();
} catch (IOException e) {	e.printStackTrace();}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 07:18 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,193
Rep Power: 4
r035198x is on a distinguished road
Default
That's a client side browser setting. You can only set the correct headers and send the file.
What the client does with it is up to them and their browser.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-09-2010, 08:24 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
tester23 is on a distinguished road
Default
But they wont be able to get the file popped up in excel unless the code is written for that, right?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-09-2010, 08:44 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,193
Rep Power: 4
r035198x is on a distinguished road
Default
All the code you can write in the servlet is going to run on the server, not on the user machine.The browser is going to act based on the headers you set. If the user machine does not have excel installed for example, the browser may simply say that the file can't be opened.
Your only bet is in playing around with headers to give the browser some hints which may be ignored.
e.g try setting a content type of "application/octet-stream" and see how the browser will respond.

The best you can do is paly around with
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-09-2010, 09:08 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
tester23 is on a distinguished road
Default
ok thats fine.. thanks for the info.. but while testing, am using the same pc as client n server. So in order to get popped up in new excel instead of getting embedded in the browser page itself, wat changes can i do?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-09-2010, 09:32 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,193
Rep Power: 4
r035198x is on a distinguished road
Default
Try the application/octet-stream like I suggested.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-09-2010, 10:12 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
tester23 is on a distinguished road
Default
nope.. the excel opens in the browser itself juz like previously.. setting contenet type will tell the browser to display the type of content.. tats al.. it wont open excel application and then show the excel.. for that, we hae to write jexcel codes in the servlet so that on click of open, excels are opened in the fly.. tats wat i learned after searching in google.. any one can help me out of this??
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-09-2010, 10:46 AM
Senior Member
 
Join Date: Aug 2009
Posts: 2,193
Rep Power: 4
r035198x is on a distinguished road
Default
Originally Posted by tester23 View Post
.... for that, we hae to write jexcel codes in the servlet so that on click of open, excels are opened in the fly.. tats wat i learned after searching in google.. any one can help me out of this??
That can't be right. jexcel codes in the servlet won't make excel available on my computer if I didn't have it and won't change my file association settings in my browser. You need to change the browser settings (on the client) to achieve that. e.g in Firefox you can go Tools->Options->Applications and set the default program to use for opening .xls files.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-25-2010, 02:58 PM
Member
 
Join Date: Feb 2010
Posts: 1
Rep Power: 0
viswa9582 is on a distinguished road
Default Hi,try this one
you shoud use "attachment" instead of "inline", then u will get the pop up window asking that open or save the file.

response.setHeader("Content-Disposition", "attachment; filename=sampleName.xls");
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i download ravi kumar New To Java 3 01-16-2010 11:40 PM
Download JDK 1.5 Nick15 New To Java 4 01-01-2009 03:10 AM
Not able to download excel sheet from client mechine using Struts and JExcel muralimohanbikki Advanced Java 0 06-21-2008 10:53 AM
What jdk do I download? padutch2 New To Java 3 11-29-2007 04:28 AM
I could download JDK 1.5 Albert New To Java 2 07-13-2007 03:36 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:08 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org