Close

PrimeFaces - DataExporter Example

[Last Updated: Jan 15, 2018]

Primefaces Datatable can be exported in various formats by using DataExporter.

Following example shows how to export data in xml, csv, pdf and excel formats.

Maven dependencies

Followings are the additional maven dependencies which are needed for xml, pdf and excel formats in the given order.

pom.xml

<dependency>
   <groupId>xmlbeans</groupId>
   <artifactId>xmlbeans</artifactId>
   <version>2.3.0</version>
</dependency>
<dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>itext</artifactId>
   <version>2.1.7</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.13</version>
</dependency>

The Managed bean

@ManagedBean
@ViewScoped
public class EmployeeBean {
  private List<Employee> employeeList = new ArrayList<>();
    .............
  public List<Employee> getEmployeeList () {
      return employeeList;
  }
}
public class Employee {
  private long id;
  private String name;
  private String phoneNumber;
  private String address;
  //getters/setters
    .............
}

The Page

src/main/webapp/index.xhtml

<h:form>
    <p:dataTable id="employeeTable"
                 var="employee"
                 value="#{employeeBean.employeeList}"
                 scrollable="true"
                 scrollHeight="300">
        <p:column headerText="Id">
            <h:outputText value="#{employee.id}"/>
        </p:column>

        <p:column headerText="Name">
            <h:outputText value="#{employee.name}"/>
        </p:column>

        <p:column headerText="Phone Number">
            <h:outputText value="#{employee.phoneNumber}"/>
        </p:column>

        <p:column headerText="Address">
            <h:outputText value="#{employee.address}"/>
        </p:column>
    </p:dataTable>
    <br/>
    <p:commandButton value="Export as XML" ajax="false" >
        <p:dataExporter type="xml" target="employeeTable" fileName="employees"/>
    </p:commandButton>
    <p:commandButton value="Export as CSV" ajax="false" >
        <p:dataExporter type="csv" target="employeeTable" fileName="employees"/>
    </p:commandButton>
    <p:commandButton value="Export Table as Excel" ajax="false" >
        <p:dataExporter type="xls" target="employeeTable" fileName="employees"/>
    </p:commandButton>
    <p:commandButton value="Export Table as PDF" ajax="false" >
        <p:dataExporter type="pdf" target="employeeTable" fileName="employees"/>
    </p:commandButton>

</h:form>

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

Exported XML File:

Exported CSV File:

Exported PDF File:

Exported XLS File:

Example Project

Dependencies and Technologies Used:

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • xmlbeans 2.3.0 xmlbeans:xmlbeans
  • itext 2.1.7: iText, a free Java-PDF library.
  • poi 3.13: Apache POI - Java API To Access Microsoft Format Files.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Primefaces Data Exporter Example Select All Download
  • table-data-exporter
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • index.xhtml

    See Also