[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

POI - writing file to servlet response

Posted on 2004-11-23
2
Medium Priority
?
2,461 Views
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/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....
0
Comment
Question by:rahulkothari
2 Comments
 
LVL 8

Expert Comment

by:kiranhk
ID: 12659093
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
 
LVL 13

Accepted Solution

by:
petmagdy earned 500 total points
ID: 12662583
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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