Go Back   Java Programming Forums > Java EE (Enterprise Edition) > Java Servlet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2010, 06:18 AM
Junior Member
 

Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
tester 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?? is fo, can anyone explain with an example??

Quote:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("fileName");
String fileDisplayName = filename.substring(filename.lastIndexOf("/")+1);
String dataFileLocation = "C:\"
File f = new File(dataFileLocation+"\\"+filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="+fileDisplayName);
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();}
}



Reply With Quote Share this thread on Facebook
  #2 (permalink)  
Old 09-02-2010, 01:01 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 861
Thanks: 31
Thanked 112 Times in 108 Posts
Json will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Help needed in excel download...

Yes, change this.

Java Code
response.setHeader("Content-Disposition", "inline; filename="+fileDisplayName);
to this:

Java Code
response.setHeader("Content-Disposition", "attachment; filename="+fileDisplayName);
Enjoy!

// Json
Reply With Quote
Sponsored Links
  #3 (permalink)  
Old 09-02-2010, 01:14 PM
Junior Member
 

Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
tester is on a distinguished road
Default Re: Help needed in excel download...

Nope.. tried that one also.. it wasnt working.. Should we have to populate the excel workbook dynamically for this using POI or jexcel?? I have no idea on that..
Reply With Quote
Reply

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
conversions and excel mkslt4 Java Theory & Questions 2 10-02-2010 06:54 AM
Facing problem with posting Excel file for download - Content in browser ragz_82 JavaServer Pages: JSP & JSTL 1 09-02-2010 01:28 PM
Importing excel data supriya ramjee File I/O & Other I/O Streams 8 20-10-2009 03:52 AM
How to Download a file via FTP JavaPF Java Code Snippets and Tutorials 0 05-06-2009 10:49 AM
Export to excel ebosysindia File I/O & Other I/O Streams 7 14-05-2009 11:25 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java convert list to map java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 02:12 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.