我正在使用Apache POI库的3.17版本来创建Java的Excel电子表格。当用户从服务器下载并尝试打开电子表格时,会显示警告消息。我认为电子表格的格式在某种程度上是不正确的。如何修复它以便用户不会看到该消息?请务必注意,电子表格打开正常,所有数据都在那里。
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet;
// Add rows and cells to spreadsheet
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=sheet.xls");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
bos.close();
try (InputStream stream = new ByteArrayInputStream(bos.toByteArray())) {
IOUtils.copy(stream, response.getOutputStream());
}
答案 0 :(得分:5)
扩展名应为.xlsx
,而不是.xls