Mohammad
Mohammad

Reputation: 97

why HSSFWorkbook.write doese not work in HttpServletResponse

I used below code to write a excel file in output stream. But when i open the downloaded file in excel it has problem with opening.

    HSSFWorkbook workbook = getWorkBook();
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) FacesContext
            .getCurrentInstance().getExternalContext().getResponse();
    response.setHeader("Content-Disposition", "attachment;filename=someName.xls");
    response.setContentLength(workbook.getBytes().length);
    response.setContentType("application/vnd.ms-excel");
    try {
        workbook.write(response.getOutputStream());
        response.getOutputStream().close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

of course i write the result in a File and it was correct.

Upvotes: 1

Views: 1129

Answers (1)

Mohammad
Mohammad

Reputation: 97

I solve this problem by removing below code.

response.setContentLength(workbook.getBytes().length);

Upvotes: 1

Related Questions