Apache POI - 使用 XSSFWorkbok + servlet 响应 [英] Apache POI - Working with XSSFWorkbok + servlet response

查看:27
本文介绍了Apache POI - 使用 XSSFWorkbok + servlet 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 应用程序无法下载 XLSX 文件时遇到问题.

按照此链接中显示的示例:创建一个excel文件供用户使用Apache POI下载,我尝试了两种配置来下载/保存电子表格.

首先使用 .XLS 文件:

response.setContentType("application/ms-excel");response.setHeader("Content-Disposition", "attachment; filename=testxls.xls");HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet 表 = wb.createSheet();HSSFRow 行 = sheet.createRow(0);HSSFCell 单元格 = row.createCell(0);cell.setCellValue("一些文字");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 行 = sheet.createRow(0);XSSFCell 单元格 = row.createCell(0);cell.setCellValue("一些文字");ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();wb.write(outByteStream);byte[] outArray = outByteStream.toByteArray();OutputStream outStream = response.getOutputStream();outStream.write(outArray);outStream.flush();

当我尝试此操作时,我收到消息:Excel 在 'testxls.xlsx' 中发现无法读取的内容.您想恢复此工作簿的内容吗?...."">

尽管有此消息,但电子表格正常打开,但我真的想删除此消息.

有什么想法吗?

解决方案

我知道这是一个非常古老的(6 年)但我在更新了一些旧代码以从 HSSF 移动到 XSSF 后来到这里,我的问题是缺少 jar xmlbeans-3.1.0.jar.该错误已写入 TomCat 本地主机日志java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject"

I'm having problems in my java application to enable downloading XLSX files.

following the example displayed in this link: Create an excel file for users to download using Apache POI, I tried two configurations to download/save a spreadsheet.

First with a .XLS file:

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();

This works.

Then i tried with a XLSX file:

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();

When i try this, i receive the message: "Excel found unreadable content in 'testxls.xlsx'. Do you want to recover the contents of this workbook? ...."

Despite this message, the spreadsheet opens normally, but i really want to remove this message.

Any ideas?

解决方案

I know this is a very old (6 years) but I made my way here after updating some old code to move from HSSF to XSSF, my issue was a missing jar xmlbeans-3.1.0.jar. The error was written to the TomCat localhost log "java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject"

这篇关于Apache POI - 使用 XSSFWorkbok + servlet 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆