在JSF 2 / Icefaces 3应用程序中导出xlsx文件

我尝试在我的JSF 2 / Icefaces 3应用程序中的xlsx文件中导出数据。 为此我创build一个工作簿,初始化行和单元格,并将其写入响应的输出stream,但我没有结果。 只有一个沙漏。 我有我的网页ajax调用,但不是在导出button。

在Firebug中,当我看到响应时,我有数据。

ManagedBean中的操作:

public String extractComments() throws TechnicalException { try { XSSFWorkbook wb= new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("First sheet"); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell(0); cell.setCellValue("Hello"); FacesContext context = FacesContext.getCurrentInstance(); ExternalContext externalContext = context.getExternalContext(); externalContext.setResponseContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + getFilename() + "\""); OutputStream out = externalContext.getResponseOutputStream(); wb.write(out); out.close(); context.responseComplete(); } catch (IOException e) { logger.error("ERROR !", e); } catch (Exception e) { logger.error("ERROR",e); } return null; } 

按键

 <ice:commandButton id="buttonExtractComments" value="#{msg['common.extract.comments']}" action="#{myBean.extractComments}" /> 

的pom.xml

 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> 

与Firebug请求:

 Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding gzip, deflate Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 Content-Length 2934 Content-Type application/x-www-form-urlencoded;charset=UTF-8 Cookie JSESSIONID=54EE464F1BE4A632EDFEFB88F46EA32D; ice.push.browser=1i3pno3mu; ice.connection.lease=1418636730869; ice.connection.contextpath=.; ice.connection.running=bc445:acquired Faces-Request partial/ajax Host 127.0.0.1:8080 Referer http://127.0.0.1:8080/myapp/pages/exctrat.xhtml User-Agent Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0 

在Firebug响应:

 Content-Disposition attachment; filename="myFile.xlsx" Content-Type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet Date Mon, 15 Dec 2014 09:45:29 GMT Server Apache-Coyote/1.1 Transfer-Encoding chunked X-UA-Compatible IE=9 

据我所知,你不能用JSF下载AJAX文件。 在Pimefaces中,您将使用ajax="false"属性来下载文件。 所以要确保你的行为不是ajax。 像这样尝试

 <f:ajax disabled="true"/>