58

I'm trying to write a program that works with Excel docs, but the HSSF format is too small for my requirements. I'm attempting to move to XSSF, but I keep getting errors when trying to use it.

I managed to solve the first two by adding xmlbeans-2.3.0.jar and dom4j-1.6.jar to my program, but now this error is coming up, which doesn't seem to be resolved by adding the Apache commons jar available on the Apache website.

The error is as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at hot.memes.ExcelCreator.main(ExcelCreator.java:66)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
f_puras
  • 2,521
  • 4
  • 33
  • 38
Cameron Zach
  • 581
  • 1
  • 4
  • 4

8 Answers8

52

Add commons-collections4-x.x.jar file in your build path and try it again. It will work.

You can download it from https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.0

Lakhwinder Sharma
  • 645
  • 1
  • 5
  • 9
  • 2
    All Apache POI dependencies are also included in the binary download package! – Gagravarr Sep 24 '16 at 10:24
  • 7
    See Anshu Kumar's answer below. The 4.0 version is not enough you need Apache Collections 4.1 version to solve this problem. I have checked. – Miklos Krivan Feb 13 '17 at 07:48
  • 1
    Namaskar mitro, commons-collections4-x.x.jar does not have ExtendedProperties.java, while commons-collections3-x.x.jar does not have LinkValuedMap.java. How did you overcome that? – Yogesh Sanchihar Nov 28 '18 at 14:26
31

commons-collections4-x.x.jar definitely solve this problem but Apache has removed the Interface ListValuedMap from commons-Collections4-4.0.jar so use updated version 4.1 it has the required classes and Interfaces.

Refer here if you want to read Excel (2003 or 2007+) using java code.

http://www.codejava.net/coding/how-to-read-excel-files-in-java-using-apache-poi

Anshu kumar
  • 407
  • 5
  • 5
  • 6
    Totally correct, have to download this version. https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1 – Federico Traiman Jan 23 '17 at 12:57
  • 3
    This helped me also many thx for it. I just have upgraded to POI 3.15 from 3.14 and identified that my XLSX exports are broken because of this. So I have changed the version from 4.0 to 4.1 and everything works well again. – Miklos Krivan Feb 13 '17 at 07:45
18

Hurrah! Adding commons-collections jar files to my project resolved this problem. Two thumbs up to Lucky Sharma.

Solution: Add commons-collections4-4.1.jar file in your build path and try it again. It will work.

You can download it from https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

Community
  • 1
  • 1
13

Please note that 4.0 is not sufficient since ListValuedMap, was introduced in version 4.1.

You need to use this maven repository link for version 4.1. Replicated below for convenience

 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
 <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.1</version>
</dependency>
HopeKing
  • 3,317
  • 7
  • 39
  • 62
5

Yeah, resolved the exception by adding commons-collections4-4.1 jar file to the CLASSPATH user varible of system. Downloaded from https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

Swetha
  • 51
  • 1
  • 1
1

XSSFWorkbook class is only available in poi-ooxml dependency. Please check if you are using poi or poi-ooxml.

implementation 'org.apache.poi:poi:5.0.0' -> HSSFWorkbook

implementation 'org.apache.poi:poi-ooxml:5.0.0' -> XSSFWorkbook

Mohan
  • 1,164
  • 15
  • 18
0

If you have downloaded pio-3.17 On eclipse: right click on the project folder -> build path -> configure build path -> libraries -> add external jars -> add all the commons jar file from the "lib". It's worked for me.

Mostafiz
  • 21
  • 1
  • 7
0

you have to add jars in your project class classpath -

  1. poi-3.17.jar
  2. poi-excelant-3.17.jar
  3. poi-ooxml-3.17.jar
  4. common-collections-4.4.jar
  5. xmlbeans-2.3.0.jar
  6. poi-ooxml-scehmas-3.17.jar
  7. common-compress.jar

There is the following example of your code.

package com.sks.xssbook;

import java.io.File;
import java.io.FileOutputStream;

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

/**
 * @author ssuman
 *
 */
public class XssBook1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try{
        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet shhet = workBook.createSheet("sheet1");
        File f1 = new File("sks.xlsx");
        FileOutputStream fileOop = new FileOutputStream(f1);
        workBook.write(fileOop);
        workBook.close();
        System.out.println("File created !!!!!");
        }
        catch(Exception ex) {
            System.err.println("Error ---- "+ex);
        }

    }

}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103