【问题标题】:PackagePropertiesMarshaller$NamespaceImpl not found using Apache poi with Java Servlet使用带有 Java Servlet 的 Apache poi 找不到 PackagePropertiesMarshaller$NamespaceImpl
【发布时间】:2019-10-09 08:46:58
【问题描述】:

我一直在尝试使用 IntelliJ 和 Tomcat 构建我的第一个 Web 应用程序,其中一项任务是能够上传和处理 Excel 工作表文件。于是,我上网查了一下,发现可以帮助我解析 Excel 文件的 Apache POI 库。但是当我下载了所有需要的jar并复制粘贴了一些测试代码,并启动服务器时,它在网页上显示http状态500的错误,根本原因是:java.lang.ClassNotFoundException:org.apache.poi .openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller$NamespaceImpl.

我遇到过其他jar的问题,但是都通过将相应的jar放在tomcat的lib文件夹中解决了,除了这个。

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

public class ExcelParser {
    private String pathname;

    public ExcelParser(String pathname) {
        this.pathname = pathname;
    }

    public void parse() {

        try {
            FileInputStream file = new FileInputStream(new File("/Users/JohnDoe/Desktop/test.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println();
            }
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我只是在测试 Excel 解析的功能,所以不用担心路径名。

顺便说一句,我可以看到这个(内部)类是在 poi-ooxml4-4.1.0.jar 中声明的,它也包含在我的 Tomcat lib 文件夹中。

感谢任何想法为什么会发生这种情况,以及我应该如何解决它。

【问题讨论】:

    标签: java tomcat servlets apache-poi classnotfoundexception


    【解决方案1】:

    要使用 Apache POI,您需要以下 jar 文件。

    1. poi-ooxml-4.1.0.jar
    2. poi-ooxml-schemas-4.1.0.jar
    3. xmlbeans-3.1.0.jar
    4. commons-compress-1.18.jar
    5. curvesapi-1.06.jar
    6. poi-4.1.0.jar
    7. commons-codec-1.12.jar
    8. commons-collections4-4.3.jar
    9. commons-math3-3.6.1.jar

    您可以参考以下链接,我已经回答了一些问题。 Unable to read Excel using Apache POI

    【讨论】:

    【解决方案2】:

    我想我在将 jar 移动到 lib 目录时遗漏了一些东西,因为我删除了原始文件并重做 cp 命令,现在一切正常。我正在以答案结束问题,感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多