We are using the Apache POI library to generate Excel file and writing out the created workbook to HttpServletResponse OutputStream to download the file.
After upgrading Apache poi version from 3.17 to 5.0.0, Excel(xlsx) file content is being corrupted.
I am seeing the below statement in version 5.0.0 change list.
Upgrade to ECMA-376 5th edition (transitional) schemas - expect API breaks when using XmlBeans directly some smaller changes are necessary when code is using the low-level CT... classes
Is it causing this issue? If yes, what changes need to done in my code?
Code:
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();
Error:
Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTIntProperty;
3.17 POI dependencies
+- 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 dependencies
+- 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