参考: https://www.cnblogs.com/cbzg/p/5747739.html
需要的jar包
网盘下载链接
提取码:qw23
读取excle文件代码
public class ReadExcel {
public static List<List<String>> readExcel(String path){
List<List<String>> list=new ArrayList<List<String>>();
try {
Workbook wb;
InputStream is=null;
try{
is=new FileInputStream(path);
//读取2007版Excel
wb=new XSSFWorkbook(is);
}catch(Exception e){
//防止异常导致输入流关闭
is=new FileInputStream(path);
//读取2003版Excel
wb=new HSSFWorkbook(is);
}
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
//读取Sheet
Sheet sheet=wb.getSheetAt(i);
if(sheet==null){
continue;
}
//处理当前页,循环每一行
for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
//得到当前行
Row row=sheet.getRow(j);
//当前行第一个单元格
int minCells=row.getFirstCellNum();
//当前行最后一个单元格
int maxCells=row.getLastCellNum();
List<String> sl=new ArrayList<String>();
for (int k = minCells; k < maxCells; k++) {
//每一个单元格
Cell cell=row.getCell(k);
if(cell==null){
continue;
}
sl.add(cell.toString());
}
list.add(sl);
}
}
if(is!=null){
is.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}