I am getting the below error when trying to read an Excel file :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I have used the below code to read Excel file with an extension of .xls. I have tried searching for answers by importing other poi jars but it didn't help, I have checked searching the web but again it didn't help.
public Object[][] getExcelData(String excelLocation, String sheetName) {
try {
Object dataSets[][] = null;
FileInputStream file = new FileInputStream(new File(excelLocation));
HSSFWorkbook workbook = new HSSFWorkbook(excelLocation);
HSSFSheet sheet = workbook.getSheet(sheetName);
int totalRowNum = sheet.getLastRowNum();
int totalColumnNum = sheet.getRow(0).getLastCellNum();
dataSets = new Object[totalRowNum][totalColumnNum];
Iterator<Row> rowIterator = sheet.iterator();
int i = 0;
while (rowIterator.hasNext()) {
i++;
HSSFRow row = (HSSFRow) rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
int j = 0;
while (cellIterator.hasNext()) {
j++;
HSSFCell cell = (HSSFCell) cellIterator.next();
switch (cell.getCellTypeEnum()) {
case STRING:
dataSets[i][j] = cell.getStringCellValue();
break;
case NUMERIC:
dataSets[i][j] = cell.getNumericCellValue();
break;
case BOOLEAN:
dataSets[i][j] = cell.getBooleanCellValue();
break;
case FORMULA:
dataSets[i][j] = cell.getCellFormula();
break;
default:
log.info("No matching ENUM Type Found");
break;
}
}
}
return dataSets;
} catch (Exception e) {
log.info(e.getCause());
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ExcelHelper excel = new ExcelHelper();
String excelLocation = ResourceHelper.getResourcePath("\\src\\main\\resources\\testData\\testData.xls");
Object[][] data = excel.getExcelData(excelLocation, "Login");
}
}