In the below code, I have used apache poi to read excel file. The code is working fine in Eclipse. But when I used maven commands to run the program, it displays below error.
package Question6;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.*;
import java.util.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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;
public class ReadAnWriteDate {
public static void main(String[] args) {
ArrayList<List> a=new ArrayList<List>();
a=read_data ("src/main/resources/input_excel/StudentDetails.xlsx", "Sheet1");
print_data(a);
}
public static ArrayList<List> read_data(String workbookPath, String sheetName){
ArrayList<List> testdata = new ArrayList<List>();
try
{
FileInputStream inputStream = new FileInputStream(new File(workbookPath));
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheet(sheetName);
//FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
int rowCount=sheet.getLastRowNum();
int columnCount=sheet.getRow(0).getLastCellNum();
for(int i=1; i<=rowCount; i++) {
List <Object> rowData=new ArrayList<Object>();
XSSFRow row = sheet.getRow(i);
for(int j=0; j<columnCount;j++) {
XSSFCell cell=row.getCell(j);
switch(cell.getCellType()) {
case STRING:
rowData.add(cell);
break;
case NUMERIC:
rowData.add(cell);
break;
}
}
testdata.add(rowData);
}
}
catch(IOException e)
{
e.printStackTrace();
}
return testdata;
}
public static void print_data(ArrayList<List> data)
{
for(int i=0;i<data.size(); i++) {
List<Object> x = new ArrayList<>();
x=data.get(i);
for (int j=0;j<x.size(); j++){
System.out.println(x.get(j));
}
System.out.println();
}
}
}
Folder stucture: folder structure image
error: error image
I have tried by added the excel fill in all the folders and in the coding by giving only file name also. But no luck
Add your resource as:
And change path to to relative so: a=read_data ("input_excel/StudentDetails.xlsx", "Sheet1"); and should be fine
Don't forget to apply maven changes on change project pom.