1

I've been doing a test class to create .xlsx files using a simple example I found. I'm not using a Maven project because the project where I'm going to implement it it's not a Maven project.

The problem is that every time I run the test program I get an error saying that there's a class missing. After look for the jar that contains the missing class and implement it, I run the program again and get the same error but missing another different class. I've already added 4 jars to the program, but every time I keep getting different missing classes. Any leads in how to make this work or if there is a more effective way to create .xlsx files using Apache POI or any other API?

Here's the code I used for the test:

public static void main(String[] args) throws FileNotFoundException, IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Java Books");
    Object[][] bookData = {{"Head First Java", "Kathy Serria", 79}, {"Effective Java", "Joshua Bloch", 36}, {"Clean Code", "Robert martin", 42}, {"Thinking in Java", "Bruce Eckel", 35}};
    int rowCount = 0;
    for (Object[] aBook : bookData) {
        Row row = sheet.createRow(++rowCount);
        int columnCount = 0;
        for (Object field : aBook) {
            Cell cell = row.createCell(++columnCount);
            if (field instanceof String) {
                cell.setCellValue((String) field);
            } else if (field instanceof Integer) {
                cell.setCellValue((Integer) field);
            }
        }
    }
    try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx")) {
        workbook.write(outputStream);
    }
}

These are the jars and exceptions that I've been getting:

img1 img2 img3 img4 img5

Added these jars, but the last exception (the one above) keeps appearing. I added the jar poi-ooxml-schemas-3.9.jar because it was supposed to be the jar that contained the missing class, but the exception persisted.

These are all the jars that I'm using: https://drive.google.com/open?id=1jFovTLN_wpCwPFL6q_HbPciU7a6wnmuv

enter image description here

  • can you share the exact error message? which class is missing? – Abi Jul 04 '19 at 00:38
  • @Abi I'm trying to upload the images, but for some reason is not letting me edit it because the code isn't idented, but I already idented it many times... – Wil Fonseca Jul 04 '19 at 00:48
  • @WilFonseca I submitted an edit to your post that displays the images and gets rid of the code formatting error. The problem was that you needed to add an `!` before each of the image links at the end. – jbinvnt Jul 04 '19 at 00:57
  • @WilFonseca I also removed an extra trailing comma near the end of the line where you declared `bookData`. Could that be what is causing the error? – jbinvnt Jul 04 '19 at 01:00
  • @jbinvnt Thank you, I'm stil new in Stackoverflow, but I haven't had that problem before. Anyway. Thanks! – Wil Fonseca Jul 04 '19 at 01:02
  • @jbinvnt I thought that was the problem as well, but even removing it didn't allowed me to update the code. – Wil Fonseca Jul 04 '19 at 01:03
  • Please show which version of org.apache.poi: poi, poi-ooxml that you are using. – Miller Cy Chan Jul 04 '19 at 01:58
  • Check https://stackoverflow.com/questions/52381075/apache-poi-java-lang-noclassdeffounderror-org-apache-commons-compress-archivers – Abi Jul 04 '19 at 07:44
  • @MillerCyChan Done, I even included all the jars I'm using. – Wil Fonseca Jul 04 '19 at 14:23
  • @Abi I downloaded and imported all the specified jars, but the las exception persisted. I uploaded all the jars that I'm using – Wil Fonseca Jul 04 '19 at 14:24
  • As you are using poi 4.1, should be poi-ooxml-schemas-4.1.jar – Miller Cy Chan Jul 05 '19 at 03:17
  • @MillerCyChan Such an easy fix and I didn't realized. Thanks, that worked perfectly! – Wil Fonseca Jul 05 '19 at 13:33

1 Answers1

1

As @Miller Cy Chan said in the comments, the answer to the problem is as simple as have the same version of jars(or at least the latest in case that not all the jars have the same version) in all the needed jars, specially the jars poi-4.1.0.jar, poi-ooxml-4.0.0.jar, poi-ooxml-schemas-4.1.jar and ooxml-schemas-1.4.