首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将apache从3.17版本升级到5.0.0版本后,xlsx文件内容已损坏

将apache从3.17版本升级到5.0.0版本后,xlsx文件内容已损坏
EN

Stack Overflow用户
提问于 2022-01-07 16:20:43
回答 1查看 718关注 0票数 0

我们使用Apache库生成Excel文件,并将创建的工作簿写入HttpServletResponse OutputStream下载该文件。

将Apache版本从3.17升级到5.0.0后,Excel(xlsx)文件内容将被破坏。

我在5.0.0版本的变更列表中看到了下面的声明。

升级到ECMA-376第5版(过渡版)模式-预期API中断时直接使用XmlBeans,一些较小的改变是必要的,当代码使用低层CT.类

是它引起了这个问题吗?如果是,在我的代码中需要做哪些更改?

代码:

代码语言:javascript
复制
final SXSSFWorkbook workbook = new SXSSFWorkbook(100);
final Sheet sheet = workbook.createSheet("Catalogue");

final CellStyle style = workbook.createCellStyle();
final Font font = workbook.createFont();
            
font.setBold(true);
font.setColor(IndexedColors.BLUE.getIndex());
style.setFont(font);
font.setFontHeightInPoints((short) 11);

int rowCount = 0;
final Row header = sheet.createRow(rowCount++);
final Cell c11 = header.createCell(0);
c11.setCellValue("Role");
c11.setCellStyle(style);

final Cell c12 = header.createCell(1);
c12.setCellValue("Business Process");
c12.setCellStyle(style);

int colcount=0;

final Row row = sheet.createRow(rowCount++);
row.createCell(colcount++).setCellValue(v.getRoleName());
row.createCell(colcount++).setCellValue(v.getBusinessProcess());

final OutputStream os = response.getOutputStream();
 
response.setContentType("application/xlsx");
response.addHeader("Content-Disposition", "attachment; filename=\"" + "RoleCatalogues" + ".xlsx\"");
OutputStream outputStream = new BufferedOutputStream(os);

workbook.write(outputStream));
response.flushBuffer();
workbook.dispose();

错误:

代码语言:javascript
复制
Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTIntProperty;

Question with same error

3.17 POI依赖项

代码语言:javascript
复制
+- org.apache.poi:poi:jar:3.17:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.10:compile
[INFO] +- org.apache.poi:poi-ooxml:jar:3.17:compile
[INFO] |  +- org.apache.poi:poi-ooxml-schemas:jar:3.17:compile
[INFO] |  |  \- org.apache.xmlbeans:xmlbeans:jar:2.6.0:compile
[INFO] |  |     \- stax:stax-api:jar:1.0.1:compile
[INFO] |  \- com.github.virtuald:curvesapi:jar:1.04:compile

5.0.0 POI依赖项

代码语言:javascript
复制
+- org.apache.poi:poi:jar:5.0.0:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.15:compile
[INFO] |  +- org.apache.commons:commons-math3:jar:3.6.1:compile
[INFO] |  \- com.zaxxer:SparseBitSet:jar:1.2:compile
[INFO] +- org.apache.poi:poi-ooxml:jar:5.0.0:compile
[INFO] |  +- org.apache.poi:poi-ooxml-lite:jar:5.0.0:compile
[INFO] |  |  \- org.apache.xmlbeans:xmlbeans:jar:4.0.0:compile
[INFO] |  |     \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |  +- org.apache.commons:commons-compress:jar:1.20:compile
[INFO] |  \- com.github.virtuald:curvesapi:jar:1.06:compile
EN

回答 1

Stack Overflow用户

发布于 2022-01-11 07:50:51

根据@AxelRichter的评论,在3.17版中,workbook.write( outputStream )在编写后自动关闭了outputStream。在较新的版本中,它必须在写入工作簿后手动关闭,如下所示:

代码语言:javascript
复制
workbook.write(outputStream);
workbook.dispose();
outputStream.close();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70624232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档