Cannot import XSSF in Apache POI

I am referencing the version 3.7 of the Apache POI and I am getting a "cannot be resolved" error when I do:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Other import statements that reference POI DO NOT give me errors, such as:

import org.apache.poi.ss.usermodel.*;

Any ideas??


Solution 1:

For OOXML to work you need the POI-OOXML jar which is separately packaged from the POI jar.

Download the POI-OOXML jar from the following location -

http://repo1.maven.org/maven2/org/apache/poi/poi-ooxml/3.11/poi-ooxml-3.11.jar

For Maven2 add the below dependency -

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>

Solution 2:

The classes for the OOXML file formats (such as XSSF for .xlsx) are in a different Jar file. You need to include the poi-ooxml jar in your project, along with the dependencies for it

You can get a list of all the components and their dependencies on the POI website here.

What you probably want to do is download the 3.11 binary package, grab the poi-ooxml jar from it, and the dependencies in the ooxml-lib directory. Import these into your project and you'll be sorted.

Alternately, if you use Maven, you can see here for the list of the artificats you'll want to depend on, but it'd want to be something like:

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.11</version>
</dependency>

The poi-ooxml maven dependency will pull in the main POI jar and the dependencies for you automatically. If you want to work with the non-spreadsheet formats, you'd also want to depend on the poi-scratchpad artifact too, as detailed on the POI components page