Results 1 to 9 of 9
  1. #1
    tester23 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    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??


    Java 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();}
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    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.

  3. #3
    tester23 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    But they wont be able to get the file popped up in excel unless the code is written for that, right?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    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

  5. #5
    tester23 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    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?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Try the application/octet-stream like I suggested.

  7. #7
    tester23 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    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??

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote 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.

  9. #9
    viswa9582 is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    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");

Similar Threads

  1. how can i download
    By ravi kumar in forum New To Java
    Replies: 3
    Last Post: 01-16-2010, 11:40 PM
  2. Download JDK 1.5
    By Nick15 in forum New To Java
    Replies: 4
    Last Post: 01-01-2009, 03:10 AM
  3. Replies: 0
    Last Post: 06-21-2008, 10:53 AM
  4. What jdk do I download?
    By padutch2 in forum New To Java
    Replies: 3
    Last Post: 11-29-2007, 04:28 AM
  5. I could download JDK 1.5
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-13-2007, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •