简体   繁体   English

如何将rich:dataTable导出为ex​​cel

[英]How to export rich:dataTable to excel

I would like to export the contents of a <rich:dataTable> or <rich:extendedDataTable> to excel. 我想将<rich:dataTable><rich:extendedDataTable>的内容导出到excel。

  • I have see that PrimeFaces has the "exporter feature" http://www.primefaces.org/showcase/ui/exporter.jsf 我看到PrimeFaces有“出口商特色” http://www.primefaces.org/showcase/ui/exporter.jsf

  • I would like to be able to do something similar to this only with out using PrimeFaces, but with richFaces (version 3.3.3)...(I would like to migrate to RichFaces 4 at some point in the future but am stuck with 3.3.3 for now) 我希望只能使用PrimeFaces做类似的事情,但是使用richFaces(版本3.3.3)...(我想在未来的某个时候迁移到RichFaces 4但是我坚持使用3.3 .3现在)

  • I have read that it is possible to build your own using http://poi.apache.org/ but I do not know where to start on implementing something like this... 我已经读过可以使用http://poi.apache.org/构建自己的,但我不知道从哪里开始实现这样的东西......

Any thoughts about how best to preform the desired export and examples would be much appreciated! 任何关于如何最好地预先形成所需出口和示例的想法将不胜感激!

Using POI in JSF isn't really different from using POI in plain Java. 在JSF中使用POI与在普通Java中使用POI没有什么不同。 Just have a collection of items representing each record. 只需拥有代表每条记录的项目集合。 You must already have it as you're using a datatable which also takes such a collection. 您必须已经拥有它,因为您正在使用也需要此类集合的数据表。 You just have to iterate over exactly the same collection to create an excel sheet in POI. 您只需迭代完全相同的集合即可在POI中创建Excel工作表。

Here's a kickoff example, where items is a List<Item> which you're also using in the datatable: 这是一个启动示例,其中itemsList<Item> ,您也在数据表中使用它:

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.

In order to offer this as a download to the JSF response, you need to provide the ExternalContext#getResponseOutputStream() as someOutputStream . 为了将此作为下载提供给JSF响应,您需要提供ExternalContext#getResponseOutputStream()作为someOutputStream You also need to set the response content type (so that the browser knows what application to associate with it) and the response content disposition (so that it's opened as an attachment and that it has a valid filename). 您还需要设置响应内容类型(以便浏览器知道要与其关联的应用程序)和响应内容处置(以便将其作为附件打开并且它具有有效的文件名)。

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 invoked to prevent JSF from performing the default navigation task (which would only corrupt the Excel file by appending some HTML output of the navigated page to the end). 最后,必须调用FacesContext#responseComplete()以防止JSF执行默认导航任务(这只会通过将导航页面的某些HTML输出附加到末尾来破坏Excel文件)。

Note that the above JSF example assumes JSF 2.x. 请注意,上面的JSF示例假定JSF 2.x. If you're actually using JSF 1.x, which lacks several convenience ExternalContext delegate methods, then you'd need to grab the raw HttpServletResponse by ExternalContext#getResponse() and perform the actions on it. 如果您实际上使用的JSF 1.x缺少一些方便的ExternalContext委托方法,那么您需要通过ExternalContext#getResponse()获取原始的HttpServletResponse并对其执行操作。 See also How to provide a file download from a JSF backing bean? 另请参见如何从JSF辅助bean提供文件下载?

I needed also this functionality so I took the primefaces dataExporter component and modified it to use it with Richfaces. 我还需要这个功能,所以我采用了primefaces dataExporter组件并修改它以与Richfaces一起使用。 I also added the capability to export collapsibleSubTable insides tables. 我还添加了导出collapsibleSubTable内部表的功能。 Primefaces and Richfaces are opensource, feel free to improve it. Primefaces和Richfaces是开源的,随时可以改进它。 Package containing sources and examples: bundle 包含源和示例的bundle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM