Apache POI java.lang.NoSuchMethodError

时间:2018-04-06 16:49:57

标签: java excel apache-poi

我的代码如下:

 import java.util.*;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.IOException;

 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.xssf.usermodel.*;

 public class ExcelRead{

     public static void main(String[] args){
        String docName = "C:\\Users\\Name\\Desktop\\excelExample.xlsx";
        try{
            InputStream xlsxDoc = new FileInputStream(docName);
            XSSFWorkbook wb = new XSSFWorkbook(xlsxDoc);
            XSSFSheet sheet = wb.getSheetAt(0);
            System.out.println(sheet.getSheetName());          
        }
        catch(Exception e){  
            e.printStackTrace();
        }
     }
}

我得到的错误代码是:

Exception in thread "main" java.lang.NoSuchMethodError: 
org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:313)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:163)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:131)
at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:112)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:83)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:128)
at org.apache.poi.openxml4j.opc.ZipPackagePart.<init>(ZipPackagePart.java:78)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:243)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:673)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:258)
at AristocratProject_1.ExcelRead.main

我添加到netbeans库的Apache依赖项是:

poi-3.17.jar
poi-ooxml-3.11.jar
xmlbeans-2.6.0.jar

从这里我几乎不知道该怎么做。谁能告诉我我的代码是错还是我需要什么其他依赖?

2 个答案:

答案 0 :(得分:0)

好的,正如rgettman解释的那样,我不得不将我的poi-ooxml-3.11.jar更新为poi-ooxml-3.17.jar,这可以通过重新加载二进制源文件并解压缩.zip文件来完成。两次更新后,我又遇到了另一个错误,这是通过添加commons-collections4-4.1.jar和poi-ooxml-schemas-3.17.jar来解决的。添加这些依赖项后,我的代码运行了。希望这有助于将来的任何人。

答案 1 :(得分:0)

  1. 如果您要使用 poi jar 和 poi-ooxml jar,请确保它们的版本相同。
  2. 您也可以在没有 poi jar 的情况下试用 poi-ooxml jar。