EasyExcel 不写对象读取表格数据得到Map类型数据
EasyExcel监听器部分
package com.qxnw.digit.scm.adm.handler;
import cn.hutool.core.convert.Convert;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.qxnw.digit.scm.adm.controller.vo.ImportUtilParam;
import com.qxnw.digit.scm.adm.service.IGsTaskConfigService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@Slf4j
public class ImportMapHandler extends AnalysisEventListener<Map<String, Object>> {
/**
* 数据库字段名和表格表头名称对应关系 对应config表template值 构造handler对象时时赋值
*/
private JSONObject headMapDB;
// 最后被真正用来作为数据处理的结果集
private List<Map<String, Object>> dataList = new ArrayList<>();
//用于存储表头的信息 其中integer值是表头对应的索引值从 0 开始 String是对应的表头的名称
private Map<Integer, String> headMap;
// 记录从表格中读取的数据总条数
private Long totalCount = 0L;
// 记录读取表格数据失败条数
private Long totalErroCount = 0L;
// 导入成功条数
private Long successCount = 0L;
// excel表格读取成功后,返回信息
private String totalMsg = "本次共导入%d条,导入成功%d条,导入失败%d条";
// 监听器的构造方法
public ImportMapHandler(JSONObject fieldsJsonObject) {
headMapDB = fieldsJsonObject;
}
// 读取表头的方法,得到表头的一个map集合 key是0为下标的索引 vaule是excel表格表头的名称
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
this.headMap=headMap;
log.info("表头信息:{}" ,headMap);
}
/**
* EasyExcel 读取数据是一行一行的进行读取
* 下面重写的方法可以理解为是对读取到的每一行的数据的处理方式
* @param valueData excel中读取的数据的集合 结构为 map 其中key为从0开始的索引
* @param context
*/
@Override
public void invoke(Map<String, Object> valueData, AnalysisContext context) {
// 总条数
totalCount++;
//把表头和值放入Map
HashMap<String, Object> paramsMap = new HashMap<>();
for(int i=0;i<valueData.size();i++){
//得到该行对应的表头名称
String key=headMap.get(i);
//得到该行第一列的数据。
Object value=valueData.get(i);
//这里是手写了一个方法来判断该值是否符合要求 如果校验不成功,则该行数据不会加入到结果集之中。
Boolean islegalValue = islegalValue(value);
if(!islegalValue){
totalErroCount++;
return;
}
//将表头作为map的key,每行每个单元格的数据作为map的value
String keyDB = getFieldsKey(headMapDB,key);
paramsMap.put(keyDB,value);
}
dataList.add(paramsMap);
successCount++;
}
private String getFieldsKey(JSONObject headMapDB, String key) {
for (String s : headMapDB.keySet()) {
if(key.equals(Convert.toStr(headMapDB.get(s)))){
return s;
}
}
return null;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
String totalMsg = String.format(this.totalMsg,this.totalCount,this.successCount,this.totalErroCount);
log.info(totalMsg);
this.totalMsg = totalMsg;
}
public List<Map<String, Object>> getResultList() {
return dataList;
}
public String getTotalMsg() {
return totalMsg;
}
public Long getTotalErroCount(){
return totalErroCount;
}
public Long getTotalSucesscount(){
return successCount;
}
/**
* 判断表格填写内容是否合法
* @param value
* @return
*/
private Boolean islegalValue(Object value){
// 正则表达式,表示字符串只能包含数字、字母和小数点和汉字
String regex = "^[\\u4e00-\\u9fa5a-zA-Z0-9.]+$";
String columnValue = Convert.toStr(value);
return Pattern.matches(regex, columnValue);
}
}
下面是读取表格的代码参考,此代码配合上述的监听器执行之后 我们就能从listener中拿到我们的表格中读取到的数据
private ImportMapHandler importMapModel(ImportTaskVo param, JSONObject fieldsJsonObject) {
// 根据前端传入的文件路径 构建一个file对象
File file = new File(param.getImportUtilParam().getFilePath());
if (!file.exists()) {
throw new BusinessException("500", "读取文件异常,请重新导入文件");
}
// 生成了继承 AnalysisEventListener的监听器
ImportMapHandler listener = new ImportMapHandler(fieldsJsonObject);
// 生成 excelReader对象 指定需要读取的文件为 file
ExcelReader excelReader = EasyExcel.read(file).build();
ReadSheet build = EasyExcel.readSheet(0).registerReadListener(listener).build();
excelReader.read(build);
excelReader.finish();
file.delete();
return listener;
}
最后在记录一下Mybatis将参数类型为Map的数据 insert 存入数据库,其中map的key为数据库对应的字段,key为数据库表对应的字段 ,value为数据库字段对应的值
private void commonMapImplement(ImportMapHandler listener, Long taskId, ImportTaskVo param, Set<String> columnList,Set<String> pageFiledList) {
List<Map<String, Object>> resultList = listener.getResultList();
// 拿到表格所有的数据
for (Map<String, Object> stringObjectMap : resultList) {
gsTaskConfigMapper.inserMap(stringObjectMap);
}
}
Mapper接口
int inserMap(@Param("param") Map<String, Object> data);
对应xml语句
INSERT INTO your_table
(
<foreach collection="param" item="value" index="key" separator=",">
${key}
</foreach>
)
values
(
<foreach collection="param" item="value" index="key" separator=",">
#{value}
</foreach>
)