wilson
wilson

Reputation: 291

primefaces datatable exporter

in XHTML page support multiple language. I use datatable show data like:

<p:column style="text-align:center; width:7%;" exportPosition="center">
      <f:facet name="header">
           <h:outputText value="#{msg['common']['identityIssuePlace']}" />
      </f:facet>
      <h:outputText value="#{element.getLocalizedName(bean.locale)}" />
</p:column>

and it show ok. but I use dataExporter to export data it throws a exception:

avax.el.PropertyNotFoundException: /backend/gms/account-manage.xhtml at line 170 and column 109 value="#{element.getLocalizedName(bean.locale)}": Property 'getLocalizedName' not found on type safp.acms.common.domain.IdentityIssuePlace

dataExporter can not binding a method? Thanks advanced.

Upvotes: 2

Views: 3007

Answers (3)

dmnoguera
dmnoguera

Reputation: 46

Solved!!!!

With this p:dataExporter does not recognize p:cellEditor

My solution it's too similar, only changes a function "exportValue" and class of extends.

Xhtml:

 <h:commandLink value="Excel" action="#{myBean.exportExcel(table, 'filename')}" />

<p:dataTable id="tablaDatos" binding="#{table}" etc...> 
<!-- data -->
</p:dataTable>

Method on bean:

public void exportExcel(DataTable table, String filename) throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    MyExporterXls exporter = new MyExporterXls();
    exporter.export(context, table, filename, false, false, "UTF-8", null, null);
    context.responseComplete();
}

My solution

public class MyExporterXls extends ExcelExporter {

    @Override
    protected String exportValue(FacesContext fc, UIComponent uic) {
        String valor = "";
        System.out.println("uic = " + uic);

        if (uic instanceof HtmlOutputText) {
            valor = parseValorCelda(((HtmlOutputText) uic).getValue());
        }
        return valor;           
    }

    private String parseValorCelda(Object valor) {
        String retorno = "";

        //Cadenas
        if (valor instanceof String) {
            retorno = (String) valor;
        }

        //Numeros ENTEROS
        if (valor instanceof Short) {
            retorno = ((Short) valor).toString();
        }

        if (valor instanceof Integer) {
            retorno = ((Integer) valor).toString();
        }

        if (valor instanceof BigInteger) {
            retorno = ((BigInteger) valor).toString();
        }

        if (valor instanceof Long) {
            retorno = ((Long) valor).toString();
        }

        //Numeros decimales
        if (valor instanceof Double) {
            retorno = ((Double) valor).toString();
        }

        if (valor instanceof Float) {
            retorno = ((Float) valor).toString();
        }

        if (valor instanceof BigDecimal) {
            retorno = ((BigDecimal) valor).toString();
        }

        return retorno;
    }

}

Upvotes: 1

Karalix
Karalix

Reputation: 141

It has been a long time since this question has been asked and the only answer given by @dmnoguera was not satifying for me.

It effectively seems that primefaces' dataExporter does not support binding on a method. But there is how I achieved to go around the problem :

Here is my column in the dataTable which is exportable :

<p:column>
    <f:facet name="header">
        <h:outputText value="#{messages['portfolioOfUser']}" />
    </f:facet>
    <h:outputText id="portfoliosOfUser"
        value="#{editPortfolio.portfoliosOfUser(listCustomerTableRow)}"
        escape="false" />
</p:column>

And here is my Bean :

public class EditPortfolioManagedBean extends EnhancedManagedBean {

    private String portfoliosOfUser ;

    public String getPortfoliosOfUser() {
        return "Coucou";
    }

    public void setPortfoliosOfUser(String portfoliosOfUser) {
        this.portfoliosOfUser = portfoliosOfUser;
    }

//Cool stuf - Start
...
//Cool stuf - End

public String portfoliosOfUser(Customer customer)
    {
        String listOfPortfolios = new String("");

        //Really sexy computation and high level algorithms

        return listOfPortfolios;

    }
}

This way I tricked dataExporter, when I export the table I have the String given by portfoliosOfUser(Customer customer) and not "Coucou". Hope it is usefull for other people !

Upvotes: 0

dmnoguera
dmnoguera

Reputation: 46

I solved it with this

Xhtml code:

<p:commandButton image="ui-icon-xls" styleClass="botonExcel" value="mylabel" ajax="false">                              
   <p:dataExporter type="xls" target=":formEditar:tablaEditar" fileName="myFilename"  postProcessor="#{parametrosMB.postProcessXLS}" />  
</p:commandButton> 

Java code:

  public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);

        //Loop for all datafiles 
        //(sheet.getLastRowNum() + 1) => because file 0 is a header
        for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
            HSSFRow header = sheet.getRow(i);              

            //Loop for each cell
            for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) {
                HSSFCell cell = header.getCell(j);                    

                //Here you need verify the cell to change value and assign new value
                cell.setCellValue("the new value!!");
            }
        }
    }

Inspirated on: http://www.primefaces.org/showcase/ui/data/dataexporter/customizedDocuments.xhtml

Sorry for my bad english

Upvotes: 1

Related Questions