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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2010, 05: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
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 09-02-2010, 12:01 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,181
Thanks: 55
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson 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
  #3 (permalink)  
Old 09-02-2010, 12: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 05:54 AM
Facing problem with posting Excel file for download - Content in browser ragz_82 JavaServer Pages: JSP & JSTL 1 09-02-2010 12: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
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int 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.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 07:06 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.