POI - writing file to servlet response

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/CreateExcel.do

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();
        workBook.write(out);
        out.flush();
        out.close();

Please help....
rahulkothariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kiranhkCommented:
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)
      {

            try
            {
                       res.setContentType("application/vnd.ms-excel");
                  res.setHeader(
                        "Content-Disposition",
                        "attachment; filename=My Result Excel.xls");

                  ServletOutputStream outStream = res.getOutputStream();

                  ArrayList resultList = result;

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

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

                  outStream.println("<table>");

                  outStream.println("</body>");
                  outStream.println("</html>");
                  outStream.flush();
                  outStream.close();

                  res.flushBuffer();

            }
            catch (IOException e)
            {
                  e.printStackTrace();
            }
      }
0
petmagdyCommented:
ok just set those headers before u getOutputStream()
      response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment; filename=excelfile.xml");        

u can also specify the download lenght  like:

      response.setContentLength(size);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.