我在java应用程序中遇到问题,无法下载XLSX文件 .
按照此链接中显示的示例:Create an excel file for users to download using Apache POI,我尝试了两种配置来下载/保存电子表格 .
首先使用.XLS文件:
response.setContentType("application/ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=testxls.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte[] outArray = outByteStream.toByteArray();
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();
这有效 .
然后我尝试使用XLSX文件:
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=testxls.xlsx");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte[] outArray = outByteStream.toByteArray();
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();
当我尝试这个时,我收到消息:“ Excel found unreadable content in 'testxls.xlsx'. Do you want to recover the contents of this workbook? .... ”
尽管有这条消息,电子表格正常打开,但我真的想删除此消息 .
有任何想法吗?
1 回答
使用此JSP代码并成功生成excel文件 . 我已通过数据库输入excel文件,您也可以手动输入 .