Abhay
Abhay

Reputation: 697

DataTable export is not working in Primefaces 4.0

I want to export a data table in csv,pdf format in primefaces 4.0 .For that I used following code :

<p:dataTable var="valuesTable" value="#{userBean.groupResultList}"
    paginator="false" id="valuesTable" resizableColumns="true"
    rendered="#{not empty userBean.groupResultList}">
    <p:column headerText="Group" id="grp">                                                  #{valuesTable.groupName}</p:column>
    <p:column headerText="Technical Knowledge" id="tk">  
                                            #{valuesTable.tkValue}
                            </p:column>
    <p:column headerText="Project Management" id="pm">  
                                                #{valuesTable.pmValue}
                            </p:column>
    <p:column headerText="Growth" id="ga">  
                                                #{valuesTable.gaValue}
                            </p:column>
    <f:facet name="footer">
        <h:commandLink>
            <p:graphicImage value="/images/xml.jpeg" width="20" height="20" />
            <p:dataExporter target="valuesTable" type="xml" fileName="Data_XML" />
        </h:commandLink>
    </f:facet>
</p:dataTable>    

Data table displays correct data in each cell when I run the application. But when I try to export it in csv or pdf format, rather then exporting actual numbers (or values of the object) it is exporting "#{valuesTable.groupName}" "#{valuesTable.tkValue}" ,"#{valuesTable.pmValue}" ,"#{valuesTable.gaValue}" for each object in csv file.

CSV file Content :

#{valuesTable.groupName}    #{valuesTable.tkFormatedValue}  #{valuesTable.pmFormatedValue}  #{valuesTable.gaFormatedValue}

Data Table Content :

Group Name  Technical Knowledge   Project Management  Growth

India            .8                       .7            1.0

I don not know why it is exporting the data table in this way..

Please Help

Thanks

Upvotes: 1

Views: 3882

Answers (2)

Pravin
Pravin

Reputation: 103

DataTable export is not working in Primefaces 4.0

Step 1: You need to add jars for exporting csv ,pdf file . itext-1.1.4.jar

Step 2: DataExplorar.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<p:panel header="Generate Report ">

<div>
<h:commandLink>
    <p:graphicImage value="/images/pdf.png"
        style="width:30px; hight:30px;" />
    <p:dataExporter type="pdf" target="tb1"
        fileName="fileName" />
</h:commandLink>

    <h:commandLink>

     <p:graphicImage value="/images/excel.png"
    style="width:25px; hight:30px;" />
  <p:dataExporter type="xls" target="tb1"
    fileName="fileName" />
   </h:commandLink>
</div>          
</p:panel>

Upvotes: 0

Serediuc Florin
Serediuc Florin

Reputation: 393

I had a similar problem once with the dataTable exporter. You have to use an outputText like this:

<h:outputText value="#{valuesTable.tkValue}"/> 

inside all the column tags, in order to see the values properly. Your column would become:

<p:column headerText="Technical Knowledge" id="tk">  
     <h:outputText value="#{valuesTable.tkValue}"/>
</p:column>

Replace all your columns and try that! Hope it helps!

And also I think the headers are not ok. You have to use an f:facet tag like this:

<p:column id="tk">
     <f:facet name="header">  
        <h:outputText value="Technical Knowledge" />  
     </f:facet>  
     <h:outputText value="#{valuesTable.tkValue}"/>
</p:column>

Now you should also see the headers in the export CSV file.

Upvotes: 3

Related Questions