2 Replies Latest reply on Jul 24, 2012 7:50 PM by deiver rovira

    export dataTable to a csv file problems with RichFaces 4.0

    deiver rovira Newbie

      Hi everybody,

      I have a problem exporting a dataTable in a CSV file. it's looks fine but when I run my application, sometimes the window for export is showed and sometines dont, anyway, when I click the export button (in my jsf page), the screen freeze and then I have to shot down the IExplorer, and I dont know what is happening, because sometimes it work's but sometimes don't....


      PLease help me....


      here is my page:


      <!-- DATA  TABLE TO EXPORT -->

      <h:panelGrid id="rtaSencilla" styleClass="panelInfo" style="text-align: center" columns="2">
                                      <rich:dataTable var="consulta" value="#{consultaMasivaMBean.listConsultamasiva}"
                                          <f:facet name="header">
                                                  <rich:column styleClass="titulotabla"><h:outputLabel value="Ciudad"/></rich:column>
                                                  <rich:column styleClass="titulotabla"><h:outputLabel value="Barrio"/></rich:column>
                                              <h:outputText value="#{consulta.cm_ciudad}" />
                                              <h:outputText value="#{consulta.cm_barrio}" />


      <!-- Button to xport -->

      <h:commandButton value="Exportar a CSV" id="exportar"






      And here is my Bean:

      public final void doExportSelectedDataToCSV() {
            final StringBuffer sb = new StringBuffer();
            for( ConsultaMasivaTable data : listConsultamasiva){

             //All my fields....
            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\"");
            //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
            try {
                //Telling the framework that the response has been completed.
            } catch (final IOException e) {
            // mange another exception





      any idea what is wrong??????




        • 1. Re: export dataTable to a csv file problems with RichFaces 4.0
          Brendan Healey Master

          >sometimes the window for export


          I don't see anything which looks like a window for export in the code, but that may not be relevant. You have a h:commandButton

          id="exportar" rendered="#{...}", do you have render="exportar" anywhere in your code? I would expect so.


          What I think could be happening is that the target of an ajax render= must be rendered when the page loads, or more precisely

          the rendered attribute must evaluate to true during the apply request values phase of the POST request. If this is not the case

          then you would just do this:


          <h:panelGroup id="exportar">

             <h:commandButton rendered="..."/>



          There are a lot of things not shown such as whether this is inside an h:form (it must be) and correct use of h:head & h:body tags

          which could also have an impact.


          I don't really know why you would see it working intermittently, but this should be a reasonable starting point.




          • 2. Re: export dataTable to a csv file problems with RichFaces 4.0
            deiver rovira Newbie

            Hi Brendan,


            I finally resolved the problem.


            Thank you for your time.