3

My code is below:

 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();
        }
     }
}

And the error code I get is:

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

The dependencies for Apache that I have added to my netbeans library are:

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

And from here I pretty much have no idea what to do. Can anyone tell me if my code is wrong or what other dependencies I need?

Fancypants753
  • 429
  • 1
  • 6
  • 19

4 Answers4

3

Ok, so as rgettman explained, I had to update my poi-ooxml-3.11.jar to poi-ooxml-3.17.jar, which can be done by just redownloading the binary source file and extracting the .zip file. After both were updated, I got another error, and this was fixed by adding the commons-collections4-4.1.jar and poi-ooxml-schemas-3.17.jar. After adding these dependencies, my code ran. Hope this helps anyone in the future.

Fancypants753
  • 429
  • 1
  • 6
  • 19
2
  1. If you are to use poi jar and poi-ooxml jar, ensure they are same version.
  2. You can also try out poi-ooxml jar without poi jar.
Demipo
  • 61
  • 3
0

This issue has to be fixed by the apache team. In any way, the quick workaround as mentioned by @Fancypants753 is to make sure that the jars are of the same version. If any of them is different, definitely this issue would occur. I recently ran into it today and stumbled upon this SO question.

In my case, I have to downgrade from 5.2.0 to 4.1.2

Before, I was running these dependencies

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

But now, I was forced to downgrade to 4.1.2 to make sure all are of the same version, and because the latest version at this moment for poi-ooxml-schemas is 4.1.2, I cannot run a version higher than this for others.

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
ken4ward
  • 2,246
  • 5
  • 49
  • 89
0

Make sure that the version displaying under the maven dependencies, and the pom.xml have the same version number.