I am kind of new to JSF Web Development. I have xhtml page that has submit. Once the user click on submit button, it export the excel page. My code is working very well with submit button,but it prints table on screen. I need help as a excel spreadsheet.
Asked
Active
Viewed 9,175 times
0
-
Here is how that is being solved with C#. Maybe it helps you. http://stackoverflow.com/questions/25012579/how-to-download-an-html-table-into-excel-file-using-asp-net-c-sharp – Christiaan Westerbeek Jun 11 '15 at 20:12
1 Answers
4
JSF doesn't provide any functionality to export data from a <h:dataTable>
into a specific format. You may use PrimeFaces (an external library) that has a <p:dataExporter>
component that provides this functionality, check the demo.
If you don't want to use PrimeFaces, then you should write the Excel file by yourself inside an action and fire a file download with the file created on the fly. Here's a small sample of how to do this.
JSF code:
<h:dataTable value="#{bean.list}" var="item">
<!-- Columns and relevant data to show to users... -->
</h:dataTable>
<br />
<h:commandLink value="Export to Excel"
action="#{exporterBean.export(bean.list)}" />
Java code:
@ManagedBean
@ApplicationScoped
public class ExporterBean {
public void export(List<?> data) {
//create the Excel file on the fly
//use Apache POI or another library to do it
//use the data in List<?> data to export the relevant data
//fire a file download...
}
}
Relevant info for this:

Community
- 1
- 1

Luiggi Mendoza
- 85,076
- 16
- 154
- 332
-
First, let me clear about the question for some of the people. I like to export the my data table in excel using JSF. – Peter Jun 12 '15 at 19:22
-
I know, and my answer explains that JSF doesn't provide an automatic option to do this. So, I provide two ways you can do it. – Luiggi Mendoza Jun 12 '15 at 19:23
-
-
That's good. Now I am confused the below part. // create the Excel file on the fly/ /use Apache POI or another library to do it //use the data in List> data to export the relevant data //fire a file download. if you have any code to explain, then I understand very well. – Peter Jun 14 '15 at 18:56