0

I try to export data in an xlsx file in my JSF 2 / Icefaces 3 application. For this i create a workbook, init rows and cells and write it in the outputstream of the response but i have no result. Only a hourglass. I have ajax call in my page but not on the export button.

In the Firebug when i look the response i have data.

Action in 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;
}

button

<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>

with Firebug Request :

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

in Firebug Response :

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
Ousmane MBINTE
  • 664
  • 4
  • 15
  • 37

1 Answers1

2

As far as i know you can't download a File with AJAX in JSF. In Pimefaces you would use ajax="false" attribute to download a file. So make sure your action is not ajax. Try like so

<f:ajax disabled="true"/> 
Kuba
  • 2,069
  • 23
  • 30