SammyRulez Projectz

Thoughts about Software Design, Java, geek stuff and more. All most daily

Monday, September 05, 2005

JSF: how to export UIData in Excel [ActionComponent]

Since I bore a lot of people in Apache myfaces mailing list with my newbbie questions I give this back to the word... hoping that it would be of some use for someone else.

This is my action bean that store the content of a uidata component in a excel file.

It needs POI ( v 2.0 in che classpath.

1 add the bean and the util class to your project

2 add the mappings to the faces-config.xml



ok properties meaning:

  • fileCache: where excel file are cached

  • templateFileName: excel file to use as a template: it could be an empty excel file, but it must be a valid excel 97/2000 ecc spreadsheet

  • sheet: sheet where to write data (number zero based)

  • rowOffset: row from where start writing data (number zero based)

3 add navigation .. here my example with a jsp displainig the link to the file...

<from-outcome>excel</from-outcome> <!-- excel is the outcome of my action! -->
<to-view-id>/excel.jsp</to-view-id><redirect />

4 add a command link to the footer (or header) facet of your dataTable

<f:facet name="footer">
<h:commandLink actionListener="#{excelManager.listenTable}" action="#{excelManager.exportData}" >
<f:param name="properties" value="name,password"/>
<f:param name="headers" value="Nome,Password"/>
<f:param name="filename" value="rapportino_excel.xls"/>
<h:outputText value="export in excel"/>

  • "properies" are the properties of the enclosed collection which is the value attribute of your dataTable you want to display in the column. (things like should work!)

  • "headers" are the header of each column

  • "filename" is the fixed part of the name of the output file. A time stamp and the dataTable id will be added.

I will make a consistent package if I will have the time this week with a decent "readme"

source code here and here


Post a Comment

<< Home