簡體   English   中英

將 apache poi 從 3.17 升級到 5.0.0 版本后 xlsx 文件內容損壞

[英]xlsx file content is corrupted after upgrading apache poi from 3.17 to 5.0.0 version

我們正在使用 Apache POI 庫來生成 Excel 文件並將創建的工作簿寫入HttpServletResponse OutputStream以下載文件。

將 Apache poi 版本從 3.17 升級到 5.0.0 后,Excel(xlsx) 文件內容已損壞。

我在 5.0.0 版更改列表中看到以下聲明。

升級到 ECMA-376 第 5 版(過渡)模式 - 預計 API 會在直接使用 XmlBeans 時中斷,當代碼使用低級 CT... 類時,需要進行一些較小的更改

是不是造成了這個問題? 如果是,我的代碼需要做哪些更改?

代碼:

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

錯誤:

Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTIntProperty;

有同樣錯誤的問題

3.17 POI依賴

+- 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 依賴

+- 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

根據@AxelRichter 的評論,在版本 3.17 中,workbook.write(outputStream) 在寫入后自動關閉了 outputStream。 在較新的版本中,必須在寫入工作簿后手動關閉,如下所示:

workbook.write(outputStream);
workbook.dispose();
outputStream.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM