在下面的代码中,我使用apache poi读取excel文件。代码在Eclipse中运行良好。但是当我使用maven命令运行程序时,它显示下面的错误。
commands used in cmd
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 structure image
错误:error image
我已经尝试通过添加Excel填写在所有的文件夹和编码中只给文件名也.但没有运气
1条答案
按热度按时间9o685dep1#
将您的资源添加为:
并将路径更改为相对路径,以便:a=read_data(“input_excel/StudentDetails.xlsx”,“Sheet1”);应该没事
不要忘记在变更项目pom中应用maven变更。