Problem exporting data to excel with <pe:exporter>

Community Driven Extensions Project
Post Reply
uther
Posts: 19
Joined: 04 Jun 2014, 22:49

07 Jul 2014, 16:20

Hi, i'm trying to exporter my datatable data to excel but the result xlsx contains references to objects instead of the data in the table, example:

DataTable

http://s15.postimg.org/7xp709vu3/exportdata.png

Excel Result

http://s29.postimg.org/7g52btf87/excel.png

Code: Select all

<p:commandLink id="xls" ajax="false">  
                            <p:graphicImage library="default" name="images/arrow-back-icon-32.png" title="Voltar"/>  
                              
                            <pe:exporter type="xlsx" target=":frmTable:grid" fileName="#{reportViewBean.currentTable.tituloTabela}"/>
                        </p:commandLink>

Code: Select all

                    <p:dataTable id="grid"
                                 sortMode="multiple" 
                                 paginator="true" 
                                 rows="10"  
                                 value="#{reportViewBean.rows}" 
                                 var="r" 
                                 filteredValue="#{reportViewBean.filtered}" 
                                 draggableColumns="true" 
                                 emptyMessage="Nenhum registro encontrado."
                                 selection="#{reportViewBean.selectedRows}"
                                 rowKey="#{r.rowkey}"
                                 rowsPerPageTemplate="10,25,50,100,200,#{fn:length(reportViewBean.rows)}"
                                 binding="#{reportViewBean.dataTable}"
                                 scrollable="true"
                                 widgetVar="reportTable"
                                 >

                        <f:facet name="header">
                            #{reportViewBean.currentTable.tituloTabela}
                        </f:facet>

                        <p:column selectionMode="multiple" width="30">

                        </p:column>

                        <p:columns value="#{reportViewBean.currentTable.colunas}" 
                                   var="c" 
                                   columnIndexVar="i" 
                                   sortBy="#{r.cells[i].descricaoCelula}" 
                                   filterBy="#{r.cells[i].descricaoCelula}"  
                                   sortFunction="#{reportViewBean.customSortF}"
                                   filterMatchMode="#{reportViewBean.columnFilters[i]}"
                                   id="th"
                                   width="200"
                                   filterStyleClass="my-filter#{i} filter">

                            <f:facet name="header">

                                <div style="float: right; vertical-align: top">
                                    <p:commandLink actionListener="#{reportViewBean.columnClickedListener(i)}" oncomplete="PF('dlg').show();" update=":frmTable:dlg" >
                                        <h:graphicImage class="filter" library="default" name="images/filter-icon.png"/>                                        
                                    </p:commandLink>

                                </div>
                                #{c.descricaoColuna}

                            </f:facet>

                            <h:panelGroup class="#{reportViewBean.getColumnClass(r.cells[i])}" layout="block">
                                <h:outputLink value="#{r.cells[i].descricaoCelula}" rendered="#{reportViewBean.isLink(r.cells[i].descricaoCelula)}">
                                    #{r.cells[i].descricaoCelula}
                                </h:outputLink>

                                <h:outputLabel value="#{r.cells[i].descricaoCelula}" rendered="#{not reportViewBean.isLink(r.cells[i].descricaoCelula)}"/>
                            </h:panelGroup>

                        </p:columns>                        




                    </p:dataTable>
Any ideas about what is wrong, i followed the primefaces showcase example...
mojarra 2.2.6
primefaces 5.0
tomcat 7.0.53

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

07 Jul 2014, 19:49

You have to use <h:outputText value=""> instead in line EL expressions.Also the outputText component should be immediate child of column component.To do export on complex table you can use custom exporter.
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

uther
Posts: 19
Joined: 04 Jun 2014, 22:49

10 Jul 2014, 15:43

Do you hava an example on how to use the custom exporter, the primefaces extensions showcase doesnt help, it only shows the bean and the page, not the implementation of the export function.
mojarra 2.2.6
primefaces 5.0
tomcat 7.0.53

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

10 Jul 2014, 17:15

Custom exporter gives you full control of code.So you can add your own functionalities in PDFExporter/ExcelExporter.

You can checkout the primefaces extensions showcase code to get full example.
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

uther
Posts: 19
Joined: 04 Jun 2014, 22:49

10 Jul 2014, 19:46

I tried the steps in the CustomExporter showcase but cant get it to work.

Here is what i did:

1 - Created a META-INF/services in resources folder
2 - Created a file named org.primefaces.extensions.component.exporter.ExporterFactory inside resources/META-INF/services
3 - added a line util.CustomExporterFactory in the file created above
4 - created the file CustomExporterFactory in the util package of my webapp with the following content:

Code: Select all

package util;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import org.primefaces.extensions.component.exporter.*;
import beans.util.ExporterController;

public class CustomExporterFactory implements ExporterFactory {

    static public enum ExporterType {
        PDF,
        XLSX
    }

    @Override
    public Exporter getExporterForType(String type) {

        Exporter exporter = null;

        FacesContext context = FacesContext.getCurrentInstance();
        ExporterController bean = (ExporterController) context.getApplication().evaluateExpressionGet(context, "#{exporterController}", ExporterController.class);
        Boolean customExport=bean.getCustomExporter();

        try {
            ExporterType exporterType = ExporterType.valueOf(type.toUpperCase());

            switch (exporterType) {

                case PDF:
                    /*if(customExport) {
                        exporter = new PDFCustomExporter();
                    }
                    else {*/
                    exporter = new PDFExporter();
                    //}
                    break;

                case XLSX:
                    if(customExport) {
                        exporter = new ExcelCustomExporter();
                    }
                    else {
                        exporter = new ExcelExporter();
                    }
                    break;


                default: {
                    /*if(customExport) {
                        exporter = new PDFCustomExporter();
                    }
                    else {*/
                        exporter = new PDFExporter();
                    //}
                    break;
                }

            }
        } catch (IllegalArgumentException e) {
            throw new FacesException(e);
        }

        return exporter;
    }
}
5 - created the ExcelCustomExporter.java file with the same content as the default ExcelExporter, just to see if it gets called
6 - created the exporterController bean to set the parameter customExport

when i click on the icon to export, its not calling the ExcelCustomExporter, its calling the default ExcelExporter, i dont know why.
mojarra 2.2.6
primefaces 5.0
tomcat 7.0.53

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

15 Jul 2014, 15:54

You commented the custom exports for both PDF and Excel.Please uncomment them and try out.
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

leoTotvs
Posts: 7
Joined: 14 Nov 2014, 22:08

27 May 2015, 14:17

sudheer wrote:You commented the custom exports for both PDF and Excel.Please uncomment them and try out.
I do this, uncomment them, but it's not work. Below is my code export:

Code: Select all

<p:commandLink id="xls" ajax="false">
						<p:graphicImage value="/resources/icones/excel-icon.png" width="50" />
						<f:setPropertyActionListener value="false"
							target="#{exporterController.customExporter}" />
						<pe:exporter type="xlsx" target=":edicao:dataTableAnual,:edicao:dataTablePeriodo,:edicao:dataTableIncidentePeriodo"
							fileName="TipoClassificação" datasetPadding="3" postProcessor="#{managerexcel.postProcessXLS}"/>
					</p:commandLink>

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

29 May 2015, 08:07

Dont raise the old threads.You just have to provide the server side component ID as target but not the absolute client ID.

Remove the customExporter flag and it was used to prevent custom exporter in the showcase demo.
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

leoTotvs
Posts: 7
Joined: 14 Nov 2014, 22:08

29 May 2015, 14:35

uther wrote:I tried the steps in the CustomExporter showcase but cant get it to work.

Here is what i did:

1 - Created a META-INF/services in resources folder
2 - Created a file named org.primefaces.extensions.component.exporter.ExporterFactory inside resources/META-INF/services
3 - added a line util.CustomExporterFactory in the file created above
4 - created the file CustomExporterFactory in the util package of my webapp with the following content:

Code: Select all

package util;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import org.primefaces.extensions.component.exporter.*;
import beans.util.ExporterController;

public class CustomExporterFactory implements ExporterFactory {

    static public enum ExporterType {
        PDF,
        XLSX
    }

    @Override
    public Exporter getExporterForType(String type) {

        Exporter exporter = null;

        FacesContext context = FacesContext.getCurrentInstance();
        ExporterController bean = (ExporterController) context.getApplication().evaluateExpressionGet(context, "#{exporterController}", ExporterController.class);
        Boolean customExport=bean.getCustomExporter();

        try {
            ExporterType exporterType = ExporterType.valueOf(type.toUpperCase());

            switch (exporterType) {

                case PDF:
                    /*if(customExport) {
                        exporter = new PDFCustomExporter();
                    }
                    else {*/
                    exporter = new PDFExporter();
                    //}
                    break;

                case XLSX:
                    if(customExport) {
                        exporter = new ExcelCustomExporter();
                    }
                    else {
                        exporter = new ExcelExporter();
                    }
                    break;


                default: {
                    /*if(customExport) {
                        exporter = new PDFCustomExporter();
                    }
                    else {*/
                        exporter = new PDFExporter();
                    //}
                    break;
                }

            }
        } catch (IllegalArgumentException e) {
            throw new FacesException(e);
        }

        return exporter;
    }
}
5 - created the ExcelCustomExporter.java file with the same content as the default ExcelExporter, just to see if it gets called
6 - created the exporterController bean to set the parameter customExport

when i click on the icon to export, its not calling the ExcelCustomExporter, its calling the default ExcelExporter, i dont know why.
Hi sudheer!

How are your f:setPropertyActionListener, the value is true (value="true")?

In The first step you created the META-INF under the folder src(or main), at the Java Resources Folder?

I did this and it was work properly!

rosière
Posts: 92
Joined: 28 Nov 2012, 15:55
Contact:

07 Jul 2015, 14:43

I also use primefaces extension's exporter, it works perfectly.
However I don't put the exporter inside the dataTable.
Instead I put it below it:

Code: Select all

 <p:dataTable *** id="myTable">
***
</p:dataTable>


            <br/>
            <h:commandLink>  
    <p:graphicImage **/>
    <pe:exporter  target="myTable" ***/>
</h:commandLink>
My exporter must stay in the same div with my dataTable. I don't prefix it with any extra characters nor did I try it.
I think that you may try to simplify your XHTML code.
JDK: 1.8
Operating system: Windows 10
server: 4.1.1.171 #badassfish (build 136)
IDE: NetBeans 11.2
primefaces : version 6.0
primefaces extensions : 4.0.0
Apache Poi: 3.15

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 17 guests