在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.pDouble(decimalFormat.format(totalSum[0]).replace(',', '.')));
        cell.setCellStyle(totalStyle);
        return workbook;
    }

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

2021-05-17 12:09:34.060 ERROR 1 --- [io-8080-exec-10] c.s.l.controller.ManagementController    : cl 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.NoClDefFoundError: Could not initialize cl org.apache.poi.xssf.model.SharedStringsTable
0

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

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

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

相关推荐

  • php与javascript的区别比较两者的优势和劣势

    示例示例PHP和都是编程语言,但它们之间存在一些重要的区别。用途:PHP主要用于服务器端编程,可以在服务器上创建动态网页,而是客户端编程语言,主要用于客户端浏览器中,可以为网页添加动态功能。…

    2023-01-21 14:03:30
    0 46 94
  • 在Docker中使用SpringBootJava生成ApachePOI xlsx文件

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

    2022-11-11 15:11:47
    0 30 93
  • cmd 执行pythonecho 'Hello World' > Hello_World.txt

    在 Windows 上,可以使用 cmd 执行 python,具体步骤如下:确保安装了 Python,并将其路径添加到系统环境变量中。…

    2023-05-23 15:39:55
    0 66 13
  • scratch与python区别开发者的不同选择

    Scratch和Python之间的主要区别在于它们的语言类型。Scratch是一种基于图形的编程语言,它使用可视化拖放模块来构建程序,而Python是一种文本型编程语言,它使用编写代码的文本语法来构建程序。…

    2023-04-18 06:12:58
    0 27 63
  • gcc编译静态库:```shellar rcs libhello.a hello.o```最终会生成一个名为libhello.a

    静态库是指在程序运行时,将一些函数和数据封装到一个文件中,并在程序运行时将其加载到内存中,以便程序调用。使用gcc编译静态库的步骤如下:…

    2023-03-31 11:07:28
    0 50 25
  • python如何处理excel表格从数据分析到自动化

    示例示例Python可以使用模块来处理Excel表格,下面是一个简单的代码示例:# 导入模块…

    2023-03-29 02:37:23
    0 33 41
  • python三大神器:使用Python的tuple、list和dict实现简单的数据结构

    示例示例Python三大神器是指:Python解释器:Python解释器是一种用于执行Python代码的程序,它可以把Python代码翻译成机器能够理解的机器语言,以便计算机能够执行。…

    2023-05-18 06:38:02
    0 74 64
  • python csv文件操作:如何使用Python处理CSV文件

    Python csv文件操作可以使用csv模块来实现。csv模块提供了一组用于处理csv文件的函数,可以方便地读取、写入csv文件。下面是一个python csv文件操作的代码示例:…

    2023-04-02 12:47:06
    0 34 34

发表评论

登录 后才能评论

评论列表(9条)