某些时候,我们使用Java得POI工具内得XSSFSheet来创建excel表格,用于导出数据,但是下载到本地用excel打开得时候就会以下提示:
解决办法:
@GetMapping("finance/exportExcel")
@ResponseBody
public ResultJson exportExcel(ReviewIExcelReq req, HttpServletResponse response) {
try {
byte[] file=financeReviewService.exportExcel(
req.getTeamId(),
req.getReviewStatus(),
req.getUserName(),
req.getClientName(),
req.getPaymentTimeStart(),
req.getPaymentTimeEnd()
);
String fileName = new String(("复核收款记录-"+DateUtil.formatDate(new Date(),"yyyyMMddhhmmss")+".xlsx").getBytes("gb2312"), "ISO8859-1");
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=/"" + fileName + "/"");
//解决Excel打开文件提示:xxx文件中部分内容有问题。是否让我们尽量尝试恢复
ByteArrayInputStream tempIn = new ByteArrayInputStream(file);
response.setHeader("Content-Length", String.valueOf(tempIn.available()));
OutputStream os=response.getOutputStream();
os.write(file);
os.flush();
return ResultJson.success("success");
} catch (Exception e) {
return ResultJson.fail(e.getMessage());
}
}
注意:以下代码为解决问题核心
//解决Excel打开文件提示:xxx文件中部分内容有问题。是否让我们尽量尝试恢复 ByteArrayInputStream tempIn = new ByteArrayInputStream(file); response.setHeader("Content-Length", String.valueOf(tempIn.available()));
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/243759.html