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 (http://jakarta.apache.org/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:
3 add navigation .. here my example with a jsp displainig the link to the file...
4 add a command link to the footer (or header) facet of your dataTable
I will make a consistent package if I will have the time this week with a decent "readme"
source code here http://www.kyub.com/ExcelManagerBean.java and here http://www.kyub.com/ReflectionUtil.java
This is my action bean that store the content of a uidata component in a excel file.
It needs POI (http://jakarta.apache.org/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
<managed-bean>
<managed-bean-name>excelManager</managed-bean-name>
<managed-bean-class>com.kyub.jsf.excelaction.ExcelManagerBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property><property-name>fileCache</property-name>
<value>/Users/sammyrulez/IdeaProjects/Faces/exploded/</value>
</managed-property><managed-property>
<property-name>templateFileName</property-name>
<value>/Users/sammyrulez/IdeaProjects/Faces/workbook.xls</value>
</managed-property>
<managed-property>
<property-name>sheet</property-name>
<value>0</value>
</managed-property>
<managed-property>
<property-name>rowOffset</property-name>
<value>3</value>
</managed-property>
</managed-bean>
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...
<navigation-case>
<from-outcome>excel</from-outcome> <!-- excel is the outcome of my action! -->
<to-view-id>/excel.jsp</to-view-id><redirect />
</navigation-case>
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"/>
</h:commandLink>
</f:facet>
- "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 order.customer.name 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 http://www.kyub.com/ExcelManagerBean.java and here http://www.kyub.com/ReflectionUtil.java