java导出excel下载后文件损坏无法修复

按照如下格式即可正常打开

XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
XSSFSheet xssfSheet = xssfWorkbook.createSheet("查询表");
。。。。
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("表名.xlsx", "UTF-8"));
ServletOutputStream out = response.getOutputStream();
xssfWorkbook.write(out);
out.close();
xssfWorkbook.close();

1、返回头设置如下:

response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("表名.xlsx", "UTF-8"));

2、如果用的XSSFWorkbook之类的,记得关闭流和工作簿

out.close();
xssfWorkbook.close();

你可能感兴趣的:(Java,java,servlet,前端)