primefaces jsf tutorial excelDo you need exporting your JSF dataTable to any kind of format such as Excel, PDF, CSV, XML ? use Primefaces to do it in a minute!

In order to export your dataTable, you can use the DataExporter UICommand which is part of the Primefaces suite. Using it is pretty simple. Required stuff to run this example:

  • Primefaces library
  • Apache POI library (dependency)
  • iText library  (dependency)

All the library can be safely added to the WEB-INF/lib folder of your Web application. In the next section of this tutorial we will show how to install Apache POI and iText as a module on JBoss AS 7.
jsf export data excel

So let's start from this example.xhtml page:

<html xmlns=""


    <h:form id="jsfexample">
        <p:panelGrid columns="2">
            <f:facet name="header">  
                Basic PanelGrid  
            <h:outputLabel for="key" value="Enter Name" />
            <p:inputText id="key" value="#{}" />

            <h:outputLabel for="value" value="Enter Surname" />
            <p:inputText id="value" value="#{manager.surname}" />

            <h:outputLabel for="age" value="Enter Age" />
            <p:inputText id="age" value="#{manager.age}" />

            <h:outputLabel for="city" value="Enter City" />
            <p:inputText id="city" value="#{}" />
            <p:commandButton action="#{}" update="mydata"
                value="Save" icon="ui-icon-check" style="margin:0" />
            <p:commandButton action="#{manager.clear}" update="mydata"
                value="Delete" icon="ui-icon-cancel" style="margin:0" />

            <h:messages />

        <br />
        <p:dataTable value="#{manager.cacheList}" var="item" id="mydata">
                <f:facet name="header">Name</f:facet>
                <h:outputText value="#{}" />
                <f:facet name="header">Surname</f:facet>
                <h:outputText value="#{item.surname}" />
                <f:facet name="header">Age</f:facet>
                <h:outputText value="#{item.age}" />
                <f:facet name="header">City</f:facet>
                <h:outputText value="#{}" />
        <p:panel header="Export All Data">
                <p:graphicImage value="/icons/excel.jpg" />
                <p:dataExporter type="xls" postProcessor="#{manager.postProcessXLS}"
                    target="mydata" fileName="myexcel" />
                <p:graphicImage value="/icons/pdf.png" />
                <p:dataExporter type="pdf" target="mydata" fileName="mypdf" />


As you can see this page contains:

  • a form for instering data
  • a datatable with id="mydata"
  • a panel used to export data in Excel or PDF

The <p:dataExporter> specifies the type of export with the "type" attribute. You can opt between "xls","pdf,"csv" and "xml". Next, you need to select the dataTable with the "target" attribute and the resulting filename with the "fileName" attribute. (In this tutorial we will show just Excel and PDF export, however using csv and xml is trivial, just add the required type attribute to the dataExporter)
Additionally an Excel, PDF dataExporter can use the preProcessor or postProcessor to add some pre-processing or post-processing functionalities to your document. This can be used to add custom styles to your document or also to modify the content as well.

For example here is the JSF bean which contains the Excel postProcessor method postProcessXLS which does some uppercase and styling to your excel:

package com.sample.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.sample.model.Person;

public class PropertyManager {

    ArrayList<Person>  cacheList  = new ArrayList ();

    private String name;
    private String surname;
    private int age;
    private String city;
        // Getters/Setters omitted for brevity

    public void save() {
        Person p = new Person(name,surname,age,city);

    public void clear() {

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

        for (Row row : sheet) {
            for (Cell cell : row) {


Here's the application in action:

primefaces jsf export excel pdf