title: Export to Excel JSF and PrimeFaces
body: <p>Using JDK 1.6, JSF 2.1, PrimeFaces 2.2.1, POI 3.2, and Apache Tomcat 7</p>
  
  <p>I am trying to setup a servlet to allow a download of an excel file based on the user selection.  The excel document is created at runtime.</p>
  
  <p>No errors and the code does get into the servlet.  </p>
  
  <p>I click the button and nothing happens.  I am not using the datatable export that PrimeFaces uses because I need to do reordering and custom formatting on the data in the Excel document.</p>
  
  <p>ExportExcelReports.java</p>
  
  <pre><code>protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {       
      response.setContentType("application/vnd.ms-excel");
      response.setHeader("Content-Disposition", "attachment; filename=\"my.xls\"");                
  
      HSSFWorkbook workbook = new HSSFWorkbook();
  
      HSSFSheet sheet = workbook.createSheet();
      HSSFRow row = sheet.createRow(0);
      HSSFCell cell = row.createCell(0);
      cell.setCellValue(0.0);
  
      FileOutputStream out = new FileOutputStream("my.xls");
      workbook.write(out);
      out.close();
  }
  </code></pre>
  
  <p>ProjectReportBean.java</p>
  
  <pre><code>public void getReportData() {
      try {
          FacesContext ctx = FacesContext.getCurrentInstance();
          ExternalContext ectx = ctx.getExternalContext();
          HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
          HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
          RequestDispatcher dispatcher = request.getRequestDispatcher("/ExportExcelReports");
          dispatcher.forward(request, response);
          ctx.responseComplete();
      } catch (Exception e) {}
  }
  </code></pre>
  
  <p>index.xhtml</p>
  
  <pre><code>&lt;h:form id="reportsForm"&gt;
      &lt;h:outputLabel for="report" value="Reports" /&gt;&lt;br /&gt;
      &lt;h:selectOneMenu id="report" value="#{projectReportBean.selectedReport}" required="true" requiredMessage="Select Report"&gt;
          &lt;f:selectItem itemLabel="---" noSelectionOption="true" /&gt;
          &lt;f:selectItems value="#{projectReportBean.reports}" /&gt;
      &lt;/h:selectOneMenu&gt;
  
      &lt;p:commandButton action="#{projectReportBean.getReportData}" value="Export" update="revgrid" /&gt;                      
  &lt;/h:form&gt;
  </code></pre>