POI - writing file to servlet response

Posted on 2004-11-23
Last Modified: 2013-11-24
I have a servlet that i use to write a excel file and then send it back to user. So the user can save it
or open it in excel. The problem is how do i give a name to this excel file. Because right now, if the
user chooses to open the excel file (rather than saving it). It takes the name of the file same as the name of the servlet
that was used to create the file...ex : http://localhost:7001/app/

Here is the snippet of the servlet . I use POI to create excel file

        HSSFWorkbook workBook = CreateExcel.writeMSExcel(list.toArray(), lst); // CreateExcel is my helper class
      // Write the output
        OutputStream out = response.getOutputStream();

Please help....
Question by:rahulkothari
    LVL 8

    Expert Comment

    If it is just excel file you need to send, then you dont have to use POI, you can directly write your html content as xls from your Servlet/ActionClass..
    following is the code

          public void generateXLS(
    ArrayList result,
                HttpServletResponse res)

                            "attachment; filename=My Result Excel.xls");

                      ServletOutputStream outStream = res.getOutputStream();

                      ArrayList resultList = result;

                            "<head><title>MY Excel  Result</title></head>");
                            ".bl2 { font-family: \"Arial\", \"Mingliu\", \"Verdana\", \"taipei\"; color: #0088cb; font-size: 12px;line-height: 135%}");
                            ".content3  { font-family: \"Arial\", \"Mingliu\", \"Verdana\", \"taipei\"; color: #333333; font-size: 12px;line-height: 120%; font-weight: normal}");
                            "<table border=\"2\" cellspacing=\"1\" cellpadding=\"3\" width=\"100%\" align=\"center\">");
                      outStream.println("<tr bgcolor=\"#FBF4C6\">");
                      for (int i = 0; i < 5; i++)
                                  "<td nowrap class='bl2' height=\"30\" bgcolor=\"#FBF4C6\" align=\"center\" width=\"3%\">");
                            outStream.println("Some Text");

                      int size = resultList.size();
                      for (int i = 0; i < size; i++)
                            outStream.println("<tr bgcolor=\"#FFFFFF\">");
                            for (int j = 0; j < 10; j++)
                                        "<td nowrap class='content3' height=\"30\" align=\"center\" width=\"3%\">");
                                  outStream.println("Your Text");




                catch (IOException e)
    LVL 13

    Accepted Solution

    ok just set those headers before u getOutputStream()
            response.setHeader("Content-Disposition","attachment; filename=excelfile.xml");        

    u can also specify the download lenght  like:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    countClumps  challenge 10 69
    hasOne  challenge 59 64
    count7 challenge 12 52
    count11 challenge 6 33
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now