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}"
                                                      rowKeyVar="numeroRegistro"
                                                      styleClass="altrowstable"
                                                      rendered="true">
                                          <f:facet name="header">
                                              <rich:columnGroup>
                                                  <rich:column styleClass="titulotabla"><h:outputLabel value="Ciudad"/></rich:column>
                                                  <rich:column styleClass="titulotabla"><h:outputLabel value="Barrio"/></rich:column>
                                              </rich:columnGroup>
                                          </f:facet>
                                          <rich:column>
                                              <h:outputText value="#{consulta.cm_ciudad}" />
                                          </rich:column>
                                          <rich:column>
                                              <h:outputText value="#{consulta.cm_barrio}" />
                                          </rich:column>
                                          </rich:dataTable>

      </h:panelGrid>

      <!-- Button to xport -->

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

                                                   action="#{consultaMasivaMBean.doExportSelectedDataToCSV}"

                                                   rendered="#{consultaMasivaMBean.showBotonExportar}"

                                                   styleClass="buttonl"></h:commandButton>

       

       

      And here is my Bean:

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

             //All my 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=\"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
            response.setContentType("application/force.download");
            //response.setContentType("application/octet-stream");
            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
            }

      }

       

       

       

      any idea what is wrong??????

       

       

      Thx.

        • 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="..."/>

          </h:panelGroup>

           

          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.

           

          Regards,

          Brendan.

          • 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.