How to export rich: dataTable to excel

I would like to export the contents of <rich:dataTable> or <rich:extendedDataTable> to excel.

  • I see PrimeFaces has an โ€œexporter functionโ€ http://www.primefaces.org/showcase/ui/exporter.jsf

  • I would like to do something like this only using PrimeFaces, but with richFaces (version 3.3.3) ... (I would like to switch to RichFaces 4 at some point in the future, but stuck with 3.3.3 for now moment)

  • I read that you can create your own using http://poi.apache.org/ , but I do not know where to start something like this ...

Any thoughts on how to best prepare the desired export and examples will be greatly appreciated!

+6
source share
2 answers

Using POIs in JSF is actually no different than using POIs in plain Java. You just have a collection of items representing each entry. You should already have this when using data that also takes such a collection. You just need to iterate over the exact same collection to create an Excel worksheet in a POI.

Here's a startup example, where items is a List<Item> , which you also use in datatable:

 Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("sheet title"); int rowIndex = 0; for (Item item : items) { Row row = sheet.createRow(rowIndex++); int columnIndex = 0; row.createCell(columnIndex++).setCellValue(item.getId()); row.createCell(columnIndex++).setCellValue(item.getName()); row.createCell(columnIndex++).setCellValue(item.getValue()); // ... } workbook.write(someOutputStream); // Write the Excel result to some output. 

To suggest this as loading a JSF response, you need to provide ExternalContext#getResponseOutputStream() as someOutputStream . You also need to set the type of response content (so that the browser knows which application is associated with it) and the location of the response content (so that it is opened as an attachment and that it has a valid file name).

 FacesContext context = FacesContext.getCurrentInstance(); ExternalContext externalContext = context.getExternalContext(); externalContext.responseReset(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment;filename=export.xls"); workbook.write(externalContext.getResponseOutputStream()); context.responseComplete(); // Prevent JSF from performing navigation. 

In the end, FacesContext#responseComplete() must be called to prevent the JSF from executing the default navigation task (which would only damage the Excel file by adding some HTML output to the navigation page at the end).

Note that the above JSF example assumes JSF 2.x. If you are actually using JSF 1.x, which lacks several ExternalContext delegate methods, you need to take the raw HttpServletResponse to ExternalContext#getResponse() and follow through on it. See Also. How to ensure that a file is loaded from the JSF bean database?

+10
source

I also need this functionality, so I took the dataExporter component and replaced it with Richfaces. I also added the ability to export SubTable folding tables. Primary and Richfaces are open source, feel free to improve it. Package containing sources and examples: package

-2
source

All Articles