[英]Apache POI - XSSFWorkbook + servlet response
I find a problem on using apache-poi.我发现使用 apache-poi 有问题。 Here is my code.
这是我的代码。
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 file is download normally, but when i open the file, the following error message display : "When i try this, i receive the message: "We found a problem with some content in 'TestingABC.xlsx'. Excel 文件正常下载,但是当我打开文件时,显示以下错误消息:“当我尝试此操作时,我收到消息:”我们发现 'TestingABC.xlsx' 中的某些内容存在问题。 Do you want us to try to recover as much as we can?
你想让我们尽量恢复吗? If you trust the source of this workbook, click 'Yes'"
如果您信任此工作簿的来源,请单击“是”
After i click 'Yes'在我点击“是”之后
the error is as follow: "Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded."错误如下:“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。”
Actually, the file data is correct, but how can i remove this message ?实际上,文件数据是正确的,但是如何删除此消息?
Thanks.谢谢。
It work after adding content Length:添加内容长度后它工作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.