1

I'm currently trying to read an excel file from java.

Therefore I downloaded Apache POI and and now trying to use this code:

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

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        try{
            readXLSXFile();
        }catch(IOException e){

        }
    }

    public static void readXLSXFile() throws IOException
    {
        InputStream ExcelFileToRead = new FileInputStream("Desktop/E_WH_2.csv");
        XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);

        XSSFWorkbook test = new XSSFWorkbook();

        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;

        Iterator rows = sheet.rowIterator();

        while (rows.hasNext())
        {
            row=(XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            while (cells.hasNext())
            {
                cell=(XSSFCell) cells.next();

                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
                {
                    System.out.print(cell.getStringCellValue()+" ");
                }
                else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
                {
                    System.out.print(cell.getNumericCellValue()+" ");
                }
                else
                {
                    //U Can Handel Boolean, Formula, Errors
                }
            }
            System.out.println();
        }

    }
}

The Excel File is always the same, it's a file with 4 columns, starting in A1 to D1 with the column names and then in A2 to D4 with the Data with an unspecified amount of rows. I also tried other pieces of code but never got it working.

With the code provided I'm getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at Main.readXLSXFile(Main.java:23)
    at Main.main(Main.java:14)
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:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

Any ideas what the problem is or anybody can provide me working code..?

Edit: just changed the path to the excel file, now I don't get any error any more, but also no output an all. Can't be like that, can it..?

Edit2: noticed that I tried to read an .csv file instead of an xls file. Now changed it to xls, but it's the same as in my first edit, I don't get any output in console (even though I don't get an error)

nameless
  • 1,483
  • 5
  • 32
  • 78
  • @user7294900 Please see my edits. – nameless Aug 28 '17 at 12:06
  • 1
    the answer is to add commons-collections4 jar – Ori Marko Aug 28 '17 at 12:07
  • What version of Apache POI are you using? In any case try to add Apache Commons Collection to your classpath: https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 – Stefano R. Aug 28 '17 at 12:08
  • @StefanoR. just added commons-collections4.4.1 now, now error changed, now it says `The supplied data apperas to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)`. What's the problem? – nameless Aug 28 '17 at 12:10
  • Okay, got it. It has to be an `xlsx` file instead of `xls`. Thank you. – nameless Aug 28 '17 at 12:12

0 Answers0