我使用下面的代码在输出流中编写了一个 excel 文件。但是当我在excel中打开下载的文件时,打开有问题。
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();
}
当然我把结果写在一个文件中,它是正确的。