2
votes

I've tried to apply apache poi but failed. I have been searching around to find the solution, yet still cannot solve the problem. Not sure whether is java version not compatible or missing jar file. I need a guidance from a professional.

Current version after system.out : 1.6.0 System.out.println(Runtime.class.getPackage().getImplementationVersion());

External Jar file added:
- dom4j-1.6.1.jar
- log4j-1.2.17.jar
- junit-4.11.jar
- poi-3.11-20141211.jar
- poi-ooxml-3.11-20141211.jar
- poi-ooxml-schemas-3.11-20141211.jar
- xmlbeans-2.6.0.jar

JSP FILE

//jsp is my front end system

<jsp:useBean id="ReadXLSXFile" scope="page" class="com.home.file.ReadXLSXFile" />
<script>
    if(sType.equals("Y"))
    {
      ReadXLSXFile.main(null);
    }
</script>

Java file

package com.home.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
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;

public class ReadXLSXFile {

public static void main(String[] args) {

    try {
        File excel = new File("C:/Banklist.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook book = new XSSFWorkbook(fis);
        XSSFSheet sheet = book.getSheetAt(0);

        Iterator<Row> itr = sheet.iterator();

        // Iterating over Excel file in Java
        while (itr.hasNext()) {
            Row row = itr.next();

            // Iterating over each column of Excel file
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.err.print(cell.getStringCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.err.print(cell.getNumericCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.err.print(cell.getBooleanCellValue() + "\t");
                    break;
                default:

                }
            }
            System.err.println("");
        }

    } catch (FileNotFoundException fe) {
        fe.printStackTrace();
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}
}

error is shown after read the excel file.xlsx enter image description here

Eclipse console result :

Caused by: java.lang.NoClassDefFoundError: org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller (initialization failure)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:87)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:258)
at com.rexit.easc.ReadXLSXFile.main(ReadXLSXFile.java:26)
at com.ibm._jsp._pop_5F_cnFW_5F_nstbl._jspService(_pop_5F_cnFW_5F_nstbl.java:500)

P/s : the question is quite similar with this thread, but I still cannot figure it out. Here

2
My best guess is you've also got an older POI version on your classpath. Can you check to see if there are any other POI jars? - Gagravarr
Thanks, problem solved. - user3835327

2 Answers

1
votes

org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller is missing in poi-3.11-20141211.jar.
First, you can check ZipPackagePropertiesMarshaller class is have in this jar.
If not have, may be your poi-3.11-20141211.jar is invalid or poi-3.11-20141211.jar is not supported for ZipPackagePropertiesMarshaller.

I would recommend you to change poi-ooxml-3.11-20141211.jar to poi-ooxml-3.9-20121203.jar.
-1
votes

use the jdk version 1.6.20 or higher to work with XSSFWorkbook. your jdk version should be higher than 1.6.20