在Docker中使用SpringBootJava生成ApachePOI xlsx文件

我有使用 iTextPdf 生成 PDF 的应用程序,现在我已经添加了一些 Apache POI 来在相同的数据上生成 Excel 文件。前面是带有 jQuery 的 JS,后端是带有 Spring Boot 的 Java 11 (openJDK),Apache POI 5.0。问题是在 IDE 中运行时一切正常。生成文件并下载没有问题。当我运行 apps docker 容器时出现问题。如果我尝试生成 Excel 文件,我得到错误

我有使用 iTextPdf 生成 PDF 的应用程序,现在我已经添加了一些 Apache POI 来在相同的数据上生成 Excel 文件。前面是带有 jQuery 的 JS,后端是带有 Spring Boot 的 Java 11 (openJDK),Apache POI 5.0。问题是在 IDE 中运行时一切正常。生成文件并下载没有问题。当我运行 apps docker 容器时出现问题。如果我尝试生成 Excel 文件,我得到错误

提前谢谢。

控制器:

try {
    Workbook workbook = excelGenerators.getExcelWorkbook(filteredList);
    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    try {
        workbook.write(boas);
    } finally {
        boas.close();
    }
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition","attachment; filename=report.xlsx");
    response.setContentLength(boas.size());
    
    OutputStream os = response.getOutputStream();
    boas.writeTo(os);
    os.flush();
    os.close();
} catch (Exception e) {
    throw new IOException(e.getMessage());
}

ExcelGenerator:

public Workbook getExcelWorkbook(List<FullOrder> orderList) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("SHEET ONE");
        sheet.createFreezePane(0, 1);
        sheet.setColumnWidth(0, 16000);
        ...
        CellStyle headerStyle = sheet.getWorkbook().createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.AQUA.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        headerStyle.setFont(font);
        Row header = sheet.createRow(0);
        Cell headerCell = header.createCell(0);
        headerCell.setCellValue("Klientas");
        headerCell.setCellStyle(headerStyle);
        ... 
        CellStyle singleOrderCellStyle = workbook.createCellStyle();
        singleOrderCellStyle.setWrapText(true);
        singleOrderCellStyle.setAlignment(HorizontalAlignment.LEFT);
        AtomicInteger rowCount = new AtomicInteger(1);
        orderList.forEach(order -> {
            Row row = sheet.createRow(rowCount.get());
            Cell cell = row.createCell(0);
            cell.setCellValue(order.get().getLabel());
            cell.setCellStyle(singleOrderCellStyle);
            ...
            rowCount.getAndIncrement();
        });
        CellStyle totalStyle = sheet.getWorkbook().createCellStyle();
        totalStyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.index);
        totalStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        totalStyle.setAlignment(HorizontalAlignment.CENTER);
        XSSFFont fontTotal = workbook.createFont();
        fontTotal.setFontName("Arial");
        fontTotal.setFontHeightInPoints((short) 12);
        fontTotal.setBold(true);
        totalStyle.setFont(fontTotal);
        Row totalRow = sheet.createRow(rowCount.get());
        Cell cell = totalRow.createCell(0);
        cell = totalRow.createCell(1);
        cell = totalRow.createCell(2);
        cell = totalRow.createCell(3);
        cell.setCellValue(totalCount[0]);
        cell.setCellStyle(totalStyle);
        cell = totalRow.createCell(4);
        cell = totalRow.createCell(5);
        cell.setCellValue(Double.parseDouble(decimalFormat.format(totalSum[0]).replace(',', '.')));
        cell.setCellStyle(totalStyle);
        return workbook;
    }

我已经改变了 catchlogger.error(e.getClass() + ": " + e.getMessage() + ": " + e.getCause(), e);,现在堆栈跟踪看起来像这样:

2021-05-17 12:09:34.060 ERROR 1 --- [io-8080-exec-10] c.s.l.controller.ManagementController    : class org.apache.poi.ooxml.POIXMLException: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
...
Caused by: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:111) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:588) ~[poi-ooxml-4.0.1.jar:4.0.1]
    ... 95 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:109) ~[poi-ooxml-4.0.1.jar:4.0.1]
    ... 96 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTIntProperty;
    at org.apache.poi.xssf.usermodel.XSSFFont.setFamily(XSSFFont.java:602) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.xssf.usermodel.XSSFFont.setFamily(XSSFFont.java:614) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.xssf.model.StylesTable.createDefaultFont(StylesTable.java:765) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.xssf.model.StylesTable.initialize(StylesTable.java:716) ~[poi-ooxml-4.0.1.jar:4.0.1]
    at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:130) ~[poi-ooxml-4.0.1.jar:4.0.1]
    ... 102 common frames omitted

在添加或更改与 apache poi 相关的 jar 到 3.17 版本后,我收到另一个错误:

java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.xssf.model.SharedStringsTable
0

解决方案是将 apache poi 版本减少到 3.17 版本,并将 xmlbeans 版本减少到 2.6.0

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(454)
AzureADMDM注册期间出现80192ee7错误
上一篇
如何获得当前月份 (october month number)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(61条)