Link to home
Start Free TrialLog in
Avatar of rahulkothari
rahulkothari

asked on

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....
Avatar of kiranhk
kiranhk

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();
            }
      }
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial