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

时间:2014-12-12 16:30:03

标签: excel jsf-2 apache-poi

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

在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

1 个答案:

答案 0 :(得分:1)

据我所知,你无法在JSF中下载带有AJAX的文件。在Pimefaces中,您将使用ajax="false"属性来下载文件。因此,请确保您的行动不是ajax。试试这样

<f:ajax disabled="true"/>