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)