EasyExcel读取数据到Map
1、Excel文件
2、EasyExcel读取数据到Map的方法
/**
* 读取excel,放入List<Map<String, String>>
* @param fileName 读取excel的文件名称
* @param sheetName sheetName
* @return datalist
*/
public static List<Map<String, Object>> readExcelToMap(String fileName, String sheetName){
List<Map<String, Object>> dataList = new ArrayList<>();
EasyExcel.read(fileName,new AnalysisEventListener<Map<String, Object>>() {
//直接使用Map来保存数据
@Override
public void invoke(Map<String, Object> data, AnalysisContext context) {
//读取到的每行数据,其key是以0开始的索引
dataList.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("Excel读取完成,文件名:"+fileName+",sheet:"+sheetName+",行数:"+dataList.size());
}
}).sheet(sheetName).doRead();
return dataList;
}
3、调用
String fileName="D:\\java_projectsdk\\maventest\\SDAutoTest-1675387862751.xls";
List<Map<String, Object>> listA=readExcelToMap(fileName,"Sheet1");
for(Map<String, Object> map:listA){
System.out.println(map);
}
4、返回结果
{0=胡**, 1=***@yahoo.com}