3 Replies Latest reply on Jul 17, 2012 7:15 PM by deiver rovira

    excel export from richfaces datatable does not contain column names

    dan zemke Newbie

      I have the h:commandButton working with an export to excel (excelExporter.export) from a richfaces datatable (rich:dataTable).  The data from the datatable is exported to excel, but not the richfaces column names.  Any insight is appreciated.



      Richfaces 3.2.2.SR1
      Seam 2.2.0.GA
      Tomcat V6.0

        • 2. Re: excel export from richfaces datatable does not contain column names
          nklashok namakkal Newbie

          I need some sample code to generate CSV file from Datatable

          • 3. Re: excel export from richfaces datatable does not contain column names
            deiver rovira Newbie

            maybe it; late but here the code you need:

            you need a commandButton in your front

            <h:commandButton value="Exportar a CSV" id="exportar"
                                                         action="#{consultaMasivaMBean.doExportSelectedDataToCSV}"
                                                         rendered="#{consultaMasivaMBean.showBotonExportar}"
                                                         styleClass="buttonl"></h:commandButton>

             

            and in your bean soeting like this:

            public final void doExportSelectedDataToCSV() {
                  final StringBuffer sb = new StringBuffer();
                  for( ConsultaMasivaTable data : listConsultamasiva){
                    sb.append(data.getCm_barrio());
                    sb.append(SEPARATOR);
                     //All your fields ...

                    sb.append("\n");
                  }
                  byte[] csvData = null;
                  // in case you need some specific charset :
                  // here is an exemple with some standard utf-8

                  try {
                      csvData = sb.toString().getBytes("utf-8");
                  } catch (final UnsupportedEncodingException e1) {
                      // manage your encoding exception error exception here
                  }
                  final FacesContext context = FacesContext.getCurrentInstance();
                  final HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

             

                  response.setHeader("Content-disposition", "attached; filename=\"consultaMasiva.csv\"");

             

                  // provided you want to ensure the file will be downloaded
                  // Some navigator may open CSV file directly if you specify this format ;
                  // up to you to do it another way if you don't mind that the navigator
                  // manages the file on itself
                  response.setContentType("application/force.download");

             

                  try {
                      response.getOutputStream().write(csvData);
                      response.getOutputStream().flush();
                      response.getOutputStream().close();
                      context.responseComplete();
                      //Telling the framework that the response has been completed.
                      FacesContext.getCurrentInstance().responseComplete();
                  } catch (final IOException e) {
                  // mange another exception
                  }