Exporting data to Excel, PDF, CVS, and XML

In this recipe, we will use the Apache MyFaces Tomahawk Sandbox to export dataTable content to an Excel or PDF document. Also, will provide a short introduction to exporting dataTable content to Excel, PDF, XML, and CSV with PrimeFaces 2.0.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 1.2, and GlassFish v3. The JSF 1.2 classes were obtained from the NetBeans JSF 1.2 bundled library. In addition, we have used Apache MyFaces Tomahawk Sandbox 1.1.9, which provides support for JSF 1.2. You can download this distribution from http://myfaces.apache.org/sandbox/index.html. The Apache MyFaces Tomahawk Sandbox libraries (including necessary dependencies) are in the book code bundle, under the /JSF_libs/Apache Tomahawk Sandbox JSF 1.2 folder.

How to do it...

For exporting the dataTable content to an Excel/PDF document we will use a dedicated component of Tomahawk Sandbox. This component is mapped by the exporterActionListener tag, which supports a set of attributes that provide us with fine control over export configuration. The following table is an overview of these attributes (you can check a detailed list of attributes in the Sandbox documentation—the following table is a snapshot of original javadoc):

Name

Required

Description

filename

No

Indicates the name of the Excel/PDF file to which the dataTable content will be exported.

fileType

No

Can be XLS or PDF. Indicates the type of export.

for

No

Indicates the id value of the dataTable to be exported.

Now, based on the description of the attributes, we have configured next exporterActionListener tag:

…
<h:form>
<t:dataTable id="my_cars" var="car" value="#{carsBean.carItems}"
preserveDataModel="false">
<h:column>
<f:facet name="header">
<h:outputText value="Car Number" />
</f:facet>
<h:outputText value="#{car.carNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Car Name" />
</f:facet>
<h:outputText value="#{car.carName}" />
</h:column>
</t:dataTable>
<h:commandButton value="Export Excel">
<s:exporterActionListener filename="output"
fileType="XLS" for="my_cars">
</s:exporterActionListener>
</h:commandButton>
<h:commandButton value="Export PDF">
<s:exporterActionListener filename="output"
fileType="PDF" for="my_cars">
</s:exporterActionListener>
</h:commandButton>
</h:form>
…

The CarBean and CarsBean beans are not really relevant here. Anyway, they can be seen in the complete code of this recipe.

In case something goes wrong, it is good to know that setting HTTP response header parameters can be the solution. Try to set them like this: Pragma to public, Cache-Control to max-age=0.

There's more...

Even if we don't present it here, is important to make you aware of the PrimeFaces 2.0 p:dataExporter component. If you want a great JSF 2.0 component for exporting your data to Excel, PDF, CSV, and XML then I'm sure that this link will be very useful to you: http://www.primefaces.org:8080/prime-showcase/ui/exporter.jsf.

Notice that you can configure p:dataExporter for three different type of type of export:

  • Excel, PDF, CSV, and XML
  • Excluding Columns
  • Customized Documents

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Extport_data_to_Excel_and_PDF.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset