NoSuchMethodError(初始化失败)使用apache poi读取excel xlsx文件
我试图应用apache poi
但失败了。 我一直在寻找解决办法,但仍然无法解决问题。 不知道是否Java版本不兼容或缺lessjar文件。 我需要一个专业的指导。
system.out之后的当前版本: 1.6.0 System.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后显示错误
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:这个问题跟这个线程很相似,但是我还是不明白。 这里
org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller在poi-3.11-20141211.jar中缺失。
首先,您可以在此jar中查看ZipPackagePropertiesMarshaller类。
如果没有,可能是您的poi-3.11-20141211.jar无效或poi-3.11-20141211.jar不支持ZipPackagePropertiesMarshaller。
I would recommend you to change poi-ooxml-3.11-20141211.jar to poi-ooxml-3.9-20121203.jar.
使用jdk 1.6.20或更高版本来处理XSSFWorkbook。 你的jdk版本应该高于1.6.20