一、根据文件名filepath获得一个Sheet
二、把sheet的每一行解析成一个List
三、关闭sheet
//拿到excel表格
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File(path));
} catch (BiffException e) {
e.printStackTrace();
error.add("文件不存在");
pushError(0, error);
return false;
} catch (IOException e) {
e.printStackTrace();
error.add("文件不存在");
pushError(0, error);
return false;
}
Sheet sheet = workbook.getSheet(getSheetIndex());二、把sheet的每一行解析成一个List
//处理excel表格
int row = 1;
int cols = cells.length;
//处理每一行
for(;row <sheet.getRows(); row++) {
List tempList = new ArrayList();
Object tempObject = null;
//处理每一列
for(int col=0; col < cols; col++) {
String content = null;;
try {
content = sheet.getCell(col, row).getContents();
} catch (Exception e) {
e.printStackTrace();
}
tempList.add(content);
}
list.add(tempList);
三、关闭sheet
workbook.close();
本文介绍了一种从Excel文件中读取数据并将其转换为List集合的方法。具体步骤包括:通过文件路径获取Workbook对象,读取指定Sheet的内容,逐行解析并将数据存储到List中。最后,确保关闭Workbook释放资源。
1286

被折叠的 条评论
为什么被折叠?



