2 Replies Latest reply on Nov 3, 2011 1:36 PM by calarcon

    export datatable to excel

    hari krishna Newbie

      Hi,

       

      How to export datatable to excel in jsf using richfaces.

       

      Is there any alternate solution to acheive it.

       

      Regards,

      hari

        • 1. Re: export datatable to excel
          h2g2 Newbie

          Hi,

           

          As far as I am concerned, there is no such component in richfaces.

          Alternately, you could use this component in MyFaces : http://myfaces.apache.org/sandbox/excelexport.html

           

          Provided your table content is mapped to some backing bean property*, the way you can do it with richFaces is to add an action (some commandLink) in your form that will trigger some exportToCSV action. Provided a simple CSV file will suit you, this could look like that :

          <h:commandLink

            action="#{contact.doExportSelectedDataToCSV}"    

            title="Export to Excel"/>

           

            private static final String     SEPARATOR         = ";";

           

            // fields list for data table

            private List<Data> allData; // + getter/setter/init/filling

           

            public final void doExportDataToCSV() {

                final StringBuffer sb = new StringBuffer();

                for(final String data : allData){

                  sb.append(data.field1);

                  sb.append(SEPARATOR);

                  sb.append(data.field2);

                  sb.append(SEPARATOR);

                  // .. and so on for all 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.reset();

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

                response.setHeader("Content-Length", String.valueOf(sb.length()));

                // 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();

                } catch (final IOException e) {

                // mange another exception

                }

            }

           

          Eventually, if you need absolutely an excel file, I suggest you refer to this page, that will provide a (more complicated) solution

           

          *if it is not, you'll have to do it first.

          • 2. Re: export datatable to excel
            calarcon Newbie

            Me pasa lo mismo... y me parece super mal que richfaces no lo tenga, como si lo hace primefaces..

            aqui esta la solución que encontre

            http://anecdotassoftware.blogspot.com/2011/11/exportar-excel-pdf-con-myfaces.html