gpt4 book ai didi

java - 使用 apache poi 读取 excel xlsx 文件时出现 NoSuchMethodError(初始化失败)

转载 作者:行者123 更新时间:2023-12-04 21:04:18 39 4
gpt4 key购买 nike

我试过申请apache poi但失败了。我一直在寻找解决方案,但仍然无法解决问题。不确定java版本是否不兼容或缺少jar文件。我需要专业人士的指导。

system.out 之后的当前版本:1.6.0System.out.println(Runtime.class.getPackage().getImplementationVersion());
添加了外部 Jar 文件:
- dom4j-1.6.1.jar
- log4j-1.2.17.jar
-junit-4.11.jar
- poi-3.11-20141211.jar
- poi-ooxml-3.11-20141211.jar
- poi-ooxml-schemas-3.11-20141211.jar
- xmlbeans-2.6.0.jar

JSP文件

//jsp is my front end system

<jsp:useBean id="ReadXLSXFile" scope="page" class="com.home.file.ReadXLSXFile" />
<script>
if(sType.equals("Y"))
{
ReadXLSXFile.main(null);
}
</script>

Java 文件
package com.home.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
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;

public class ReadXLSXFile {

public static void main(String[] args) {

try {
File excel = new File("C:/Banklist.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(0);

Iterator<Row> itr = sheet.iterator();

// Iterating over Excel file in Java
while (itr.hasNext()) {
Row row = itr.next();

// Iterating over each column of Excel file
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.err.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.err.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.err.print(cell.getBooleanCellValue() + "\t");
break;
default:

}
}
System.err.println("");
}

} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}

读取 excel file.xlsx 后显示错误
enter image description here

Eclipse 控制台结果:
Caused by: java.lang.NoClassDefFoundError: org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller (initialization failure)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:87)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:258)
at com.rexit.easc.ReadXLSXFile.main(ReadXLSXFile.java:26)
at com.ibm._jsp._pop_5F_cnFW_5F_nstbl._jspService(_pop_5F_cnFW_5F_nstbl.java:500)

P/s:这个问题与这个线程非常相似,但我仍然无法弄清楚。 Here

最佳答案

poi-3.11-20141211.jar 中缺少 org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller。

首先,您可以检查 ZipPackagePropertiesMarshaller 类是否在此 jar 中。
如果没有,可能是您的 poi-3.11-20141211.jar 无效或 ZipPackagePropertiesMarshaller 不支持 poi-3.11-20141211.jar。

I would recommend you to change poi-ooxml-3.11-20141211.jar to poi-ooxml-3.9-20121203.jar.

关于java - 使用 apache poi 读取 excel xlsx 文件时出现 NoSuchMethodError(初始化失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145857/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com