【发布时间】:2020-11-11 17:16:57
【问题描述】:
我发现使用 apache-poi 有问题。这是我的代码。
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(request.getSession().getServletContext().getRealPath("/Testing.xlsx")));
XSSFSheet spreadsheet = workbook.getSheet("TempSheet");
File template = new File(request.getSession().getServletContext().getRealPath("/Testing.xlsx"));
FileOutputStream out = new FileOutputStream(template);
workbook.write(out);
out.close();
String currentTime = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
String fileName = "TestingABC.xlsx";
String absoluteDiskPath = request.getSession().getServletContext().getRealPath("/Testing.xlsx");
File f = new File(absoluteDiskPath);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
try {
byte[] outputByte = new byte[4096];
while (in.read(outputByte, 0, 4096) != -1) {
outs.write(outputByte, 0, 4096);
}
outs.flush();
outs.close();
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if(outs != null)
outs.close();
if(in != null)
in.close();
}catch (Exception ioe2) {
ioe2.printStackTrace();
}
}
}
Excel文件可以正常下载,但是当我打开文件时,显示以下错误信息: “当我尝试这个时,我收到消息:“我们发现 'TestingABC.xlsx' 中的某些内容存在问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是””
点击“是”后
错误如下: “Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。”
其实文件数据是正确的,但是如何去掉这条信息呢?
谢谢。
【问题讨论】:
-
请查看我关于此的类似帖子,并在可能的情况下给予支持;)
-
你提到的帖子有链接吗?
-
这里是:stackoverflow.com/questions/64666494/…(这是我的最后一个问题)
标签: java excel servlets apache-poi xlsx