Hitachi Vantara Pentaho Community Forums
Results 1 to 3 of 3

Thread: Excel format in servlet

  1. #1
    Join Date
    Mar 2003
    Posts
    4,947

    Default Excel format in servlet

    Hi,

    T try to generate an Excel stream with a servlet.
    But, there is no example in the jfree ext.
    I tried to use the HTMLservlet as starting point as you said in a previous topic, but i can't find the solution.


    If someone had already done that, let me know.
    Thank you in advance for your help.

    Damien

  2. #2
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    Hi,

    ok, this is code from the JFreeReportHtmlServlet, modified to export excel documents:

    Code:
      public void doPost(final HttpServletRequest request, final HttpServletResponse response)
          throws ServletException, IOException
      {
        Log.debug("in processRequest..." + getClass());
    
        final URL in = getClass().getResource("/com/jrefinery/report/demo/swing-icons.xml");
        if (in == null)
        {
          throw new ServletException("Missing Resource: /com/jrefinery/report/demo/swing-icons.xml");
        }
    
        final URL base = getServletContext().getResource("/WEB-INF/lib/jlfgr-1_0.jar");
        Log.debug("Base: " + base);
        final AbstractTableReportServletWorker worker =
            new DefaultTableReportServletWorker(in, new DemoModelProvider(base));
    
        // display the content in the browser window (see RFC2183)
        // may or may not work for excel content ...
        response.setHeader("Content-Disposition", "inline; filename=\"" + "unknown.xls" + "\"");
        response.setHeader("Content-Type", "application/vnd.ms-excel");
    
        try
        {
          // this throws an exception if the report could not be parsed
          final ExcelProcessor processor = new ExcelProcessor(worker.getReport());
          processor.setOutputStream(response.getOutputStream());
          worker.setTableProcessor(processor);
        }
        catch (Exception e)
        {
          Log.debug("Failed to parse the report", e);
          response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          return;
        }
    
        try
        {
          worker.processReport();
        }
        catch (Exception e)
        {
          Log.debug("Failed to create the report", e);
        }
      }
    The only lines that changed where the lines where the content type is set and where the used table processor implementation was defined.

    Now we use an excel processor instead of the html processor, everything else remained the same.

    Have more fun,
    said Thomas

  3. #3
    Join Date
    Mar 2003
    Posts
    4,947

    Default

    Thank you very much,

    Damien

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.