0

I have server side webapp and I am trying to generate an excel file with Apache POI, which is provided as download by backing bean. I am able to write the file on server disk, but not to write it to HTTP response.

Here's my code

public void createExcel(){
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet("hello world");
        HSSFRow row = sheet.createRow((short) 0);
        HSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue(new HSSFRichTextString("Hello"));
        cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("world"));

        workBook.write(bos);
        bos.flush();

        FacesContext fc = FacesContext.getCurrentInstance();

        downloadExcel(bos, fc);
    } catch (FileNotFoundException e) {
        log.debug("file not found ", e);
    } catch (IOException e){
        log.debug("IOException ", e);
    }
}

public void downloadExcel(ByteArrayOutputStream baos, FacesContext fc){
    HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
    response.setContentType("application/vnd.ms-excel");
    response.addHeader("Content-Disposition", "attachment; filename=testxls.xls");

    try {
        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);

        out.flush();
        out.close();
    } catch (IOException e) {
        log.error("IOException ", e);
    }
    fc.responseComplete();
}

And the result is following

response

This happens in all browsers I tried: Chrome, FF and IE. I hope you can point out what I am missing here.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Üllar
  • 27
  • 7
  • Result illegible. Don't post pictures of text. Post the text. Have you considered generating CSV? Infinitely simpler, and Excel will convert it at the client. – user207421 Nov 29 '15 at 21:49
  • Press F12 in browser, open "Network" tab, click the request representing the file download and look at HTTP response headers. Which `Content-Type` header exactly is actually being set? Nonetheless, is one of those QA helpful? http://stackoverflow.com/q/9391838 or http://stackoverflow.com/q/7822758? – BalusC Nov 30 '15 at 09:02
  • Response headers are just like i set them. Looked through your given QAs and added some minor changes to my response(reset, contentLength), but result is still the same. – Üllar Nov 30 '15 at 09:41

2 Answers2

0

Finally found the solution in BalusC given link. I called createExcel() method in JSF inside <a4j:commandButton> tag, which called method through ajax. Changed it to <h:commandButton> and now it is working.

Üllar
  • 27
  • 7
-1

The browser does not ever open a dialog to invite you to download the file when you navigate your servlet? I guess the reason is that you missed to enclose the filename between quotes:

response.addHeader("Content-Disposition", "attachment; filename=\"testxls.xls\"");
Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • Browser does not open a download dialog and modifying header changed nothing. – Üllar Nov 30 '15 at 09:42
  • That's nonsense. The program seems to be OK. Have you done more laboratory tests? Changing the binary data by some plain text message? – Little Santi Nov 30 '15 at 14:04