0

I've been trying to build my first web application using IntelliJ and Tomcat, and one of the tasks is being able to upload and process an Excel sheet file. So, I looked up online, and found the Apache POI library that can help me parse an Excel file. But when I downloaded all the required jars and copied and pasted some testing code, and start up the server, it shows on the webpage an error with http status 500, the root cause being: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller$NamespaceImpl.

I've encountered the problem with other jars, but all solved by putting the corresponding jars inside tomcat's lib folder, just except for this one.

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

public class ExcelParser {
    private String pathname;

    public ExcelParser(String pathname) {
        this.pathname = pathname;
    }

    public void parse() {

        try {
            FileInputStream file = new FileInputStream(new File("/Users/JohnDoe/Desktop/test.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println();
            }
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm just testing the functionality of Excel parsing, so don't really worry about the pathname.

Btw, I can see that this (inner) class is declared in poi-ooxml4-4.1.0.jar, which is also included in my Tomcat lib folder.

Any ideas why this is happening, and how I should fix it is appreciated.

2 Answers2

1

To use Apache POI, you need the following jar files.

  1. poi-ooxml-4.1.0.jar
  2. poi-ooxml-schemas-4.1.0.jar
  3. xmlbeans-3.1.0.jar
  4. commons-compress-1.18.jar
  5. curvesapi-1.06.jar
  6. poi-4.1.0.jar
  7. commons-codec-1.12.jar
  8. commons-collections4-4.3.jar
  9. commons-math3-3.6.1.jar

You can refer to the following link, which I have answered few things. Unable to read Excel using Apache POI

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • Thanks for your reply, but all these jars exist in the tomcat/lib directory, also, as I mentioned, the missing class, as far as I know resides inside the poi-ooxml-4.1.0.jar which again, has been copied to the tomcat/lib folder. – Wenhao Ryan May 22 '19 at 20:02
  • See also [the official Apache POI documentation on the dependencies](http://poi.apache.org/components/index.html#components) – Gagravarr May 22 '19 at 21:31
0

I think I missed something when moving the jars to the lib directory, as I removed the original files and redo the cp command, everything works now. I'm closing the question with answer, thanks for the help!