导入excel的时候,除了数据库校验数据以外,还会碰到数据格式校验,如果直接报错,显得客户体验很不好,我想到的做法是,通过java自带的validation+正则表达式先去判断excel每一行数据是否符合格式,然后通过业务校验,导入正确的数据,错误的数据重新导出,并在后面加上错误信息。
环境准备:
spring:springboot2.X
easyExcel:2.1.4
lombok(非必须)
easyExcel官网:https://alibaba-easyexcel.github.io/index.html
参考代码:https://github.com/zhyhuayong/easyexcelTest
代码
引入easyExcel、lombok、fastjson等依赖
<!--easyExcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
这里需要注意的是各个版本的poi需要对应,如果版本不对会导致代码出错。
通过easyExcel官方文档我们知道,easyExcel有一个监听器,用来读取excel数据,并且监听器不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
下面是我的监听器的写法
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.util.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.lang.reflect.Field;
import java.util.*;
/**
* @author zhy
* @title: EasyExcelListener
* @projectName cec-moutai-bd-display
* @description: easyExcel监听器
* @date 2019/12/2318:28
*/
@Data
@EqualsAndHashCode(callSuper=false)
public class EasyExcelListener <T> extends AnalysisEventListener<T> {
//成功结果集
private List<T> successList = new ArrayList<>();
//失败结果集
private List<ExcelCheckErrDto<T>> errList = new ArrayList<>();
//处理逻辑service
private ExcelCheckManager<T> excelCheckManager;
private List<T> list = new ArrayList<>();
//excel对象的反射类
private Class<T> clazz;
public EasyExcelListener(ExcelCheckManager<T> excelCheckManager){
this.excelCheckManager = excelCheckManager;
}
public EasyExcelListener(ExcelCheckManager<T> excelCheckManager,Class<T> clazz){
this.excelCheckManager = excelCheckManager;
this.clazz = clazz;
}
@Override
public void invoke(T t, AnalysisContext analysisContext) {
String errMsg;
try {
//根据excel数据实体中的javax.validation + 正则表达式来校验excel数据
errMsg = EasyExcelValiHelper.validateEntity(t);
} catch (NoSuchFieldException e) {
errMsg = "解析数据出错";
e.printStackTrace();
}
if (!StringUtils.isEmpty(errMsg)){
ExcelCheckErrDto excelCheckErrDto = new ExcelCheckErrDto(t, errMsg);
errList.add(excelCheckErrDto);
}else{
list.add(t);
}
//每1000条处理一次
if (list.size() > 1000){
//校验
ExcelCheckResult result = excelCheckManager.checkImportExcel(list);
successList.addAll(result.getSuccessDtos());
errList.addAll(result.getErrDtos());
list.clear();
}
}
//所有数据解析完成了 都会来调用
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
ExcelCheckResult result = excelCheckManager.checkImportExcel(list);
successList.addAll(result.getSuccessDtos());
errList.addAll(result.getErrDtos());
list.clear();
}
/**
* @description: 校验excel头部格式,必须完全匹配
* @param headMap 传入excel的头部(第一行数据)数据的index,name
* @param context
* @throws
* @return void
* @author zhy
* @date 2019/12/24 19:27
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
super.invokeHeadMap(headMap, context);
if (clazz != null){
try {
Map<Integer, String> indexNameMap = getIndexNameMap(clazz);
Set<Integer> keySet = indexNameMap.keySet();
for (Integer key : keySet) {
if (StringUtils.isEmpty(headMap.get(key))){
throw new ExcelAnalysisException("解析excel出错,请传入正确格式的excel");
}
if (!headMap.get(key).equals(indexNameMap.get(key))){
throw new ExcelAnalysisException("解析excel出错,请传入正确格式的excel");
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
/**
* @description: 获取注解里ExcelProperty的value,用作校验excel
* @param clazz
* @throws
* @return java.util.Map<java.lang.Integer,java.lang.String>
* @author zhy
* @date 2019/12/24 19:21
*/
@SuppressWarnings("rawtypes")
public Map<Integer,String> getIndexNameMap(Class clazz) throws NoSuchFieldException {
Map<Integer,String> result = new HashMap<>();
Field field;
Field[] fields=clazz.getDeclaredFields();
for (int i = 0; i <fields.length ; i++) {
field=clazz.getDeclaredField(fields[i].getName());
field.setAccessible(true);
ExcelProperty excelProperty=field.getAnnotation(ExcelProperty.class);
if(excelProperty!=null){
int index = excelProperty.index();
String[] values = excelProperty.value();
StringBuilder value = new StringBuilder();
for (String v : values) {
value.append(v);
}
result.put(index,value.toString());
}
}
return result;
}
}
从上到下分别解释一下,或者贴一下代码
- 失败结果ExcelCheckErrDto,里面存放的是校验失败的excel实体+错误信息
import lombok.Data;
/**
* @author zhy
* @title: ExcelCheckErrDto
* @projectName cec-moutai-bd-display
* @description: excel数据导入错误结果(单条)
* @date 2019/12/2318:23
*/
@Data
public class ExcelCheckErrDto<T> {
private T t;
private String errMsg;
public ExcelCheckErrDto(){}
public ExcelCheckErrDto(T t, String errMsg){
this.t = t;
this.errMsg = errMsg;
}
}
- excel正则校验方法EasyExcelValiHelper的写法,该方法会根据实体类中的注解来通过正则表达式判断当前单元格内的数据是否符合标准,例如只能是数字之类的,返回的是检查的错误信息
import com.alibaba.excel.annotation.ExcelProperty;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.lang.reflect.Field;
import java.util.Set;
public class EasyExcelValiHelper {
private EasyExcelValiHelper(){}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
public static <T> String validateEntity(T obj) throws NoSuchFieldException {
StringBuilder result = new StringBuilder();
Set<ConstraintViolation<T>> set = validator.validate(obj, Default.class);
if (set != null && !set.isEmpty()) {
for (ConstraintViolation<T> cv : set) {
Field declaredField = obj.getClass().getDeclaredField(cv.getPropertyPath().toString());
ExcelProperty annotation = declaredField.getAnnotation(ExcelProperty.class);
//拼接错误信息,包含当前出错数据的标题名字+错误信息
result.append(annotation.value()[0]+cv.getMessage()).append(";");
}
}
return result.toString();
}
}
正则校验完毕之后就是业务校验,通过service方法进行业务校验,校验的service接口必须继承ExcelCheckManager类,在实现类中实现checkImportExcel方法
- excel数据业务校验接口ExcelCheckManager
import java.util.List;
/**
* @author zhy
* @title: ExcelCheckManager
* @projectName cec-moutai-bd-display
* @description: excel校验接口
* @date 2019/12/2317:44
*/
public interface ExcelCheckManager<T> {
/**
* @description: 校验方法
* @param objects
* @throws
* @return com.cec.moutai.common.easyexcel.ExcelCheckResult
* @author zhy
* @date 2019/12/24 14:57
*/
ExcelCheckResult checkImportExcel(List<T> objects);
}
需要校验excel业务的service接口可以继承这个接口,并在实现类中实现自己的方法,返回的是ExcelCheckResult,里面包含成功+失败的结果集,当然可以不必返回成功结果集,一般的处理就是业务校验通过的数据可以直接持久化操作,也可以放在successDtos里面到最后进行批量操作,但是要注意的是oom
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhy
* @title: ExcelCheckErrDto
* @projectName cec-moutai-bd-display
* @description: excel数据导入结果
* @date 2019/12/2318:23
*/
@Data
public class ExcelCheckResult<T> {
private List<T> successDtos;
private List<ExcelCheckErrDto<T>> errDtos;
public ExcelCheckResult(List<T> successDtos, List<ExcelCheckErrDto<T>> errDtos){
this.successDtos =successDtos;
this.errDtos = errDtos;
}
public ExcelCheckResult(List<ExcelCheckErrDto<T>> errDtos){
this.successDtos =new ArrayList<>();
this.errDtos = errDtos;
}
}
- 测试pojo
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @title: ExcelImportTest
* @projectName easyexceldemo
* @description: 用户
* @author zhy
* @date 2020/1/1610:41
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
//名称
private String name;
//性别
private String sex;
//年龄
private Integer age;
//生日
@JSONField(format="yyyy-MM-dd HH:mm:ss")
@JsonFormat( pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date birthday;
excel数据实体类(数据字段名和pojo的一样,但是要注意的是,其属性类型都为String类型,方便正则判断)
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.zhy.easyexceldemo.easyexcel.ExcelPatternMsg;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
/**
* @title: UserExcelDto
* @projectName easyexceldemo
* @description: 用户excel类
* @author zhy
* @date 2020/1/1610:45
*/
@Data
public class UserExcelDto implements Serializable {
private static final long serialVersionUID = 1L;
//名称
@ExcelProperty(index = 0,value = "名称")
@ColumnWidth(30)
@Length(max = 10)
private String name;
//性别
@ExcelProperty(index = 1,value = "性别")
@ColumnWidth(30)
@Length(max = 2)
private String sex;
//年龄
@ExcelProperty(index = 2,value = "年龄")
@ColumnWidth(30)
@Pattern(regexp = ExcelPatternMsg.NUMBER,message = ExcelPatternMsg.NUMBER_MSG)
private String age;
//生日
@ExcelProperty(index = 3,value = "生日")
@Pattern(regexp = ExcelPatternMsg.DATE_TIME1,message = ExcelPatternMsg.DATE_TIME1_MSG)
private String birthday;
@ExcelProperty是easyExcel自带的注解
@ColumnWidth也是easyExcel的注解代表单元格宽度
@Length代表的是字符串长度,max代表的是最长允许多长
@Pattern就是正则表达式注解了,regexp代表的是正则表达式,message代表是,没有匹配成功返回的错误信息
关于validation的注解可以参考此篇大佬的博客:https://blog.csdn.net/weixin_42546729/article/details/89364431
可以发现,我这里的接收对象都是String类型的,也正因为是这样,才能通过正则表达式去校验各种格式。
所以在excel类和真正的实体类之间的转换,我是用fastjson的JSON.parseObject来进行转换的。所以需要保证excel类和数据库实体的字段名要保持一致,数据格式,尤其是日期格式,要保持一致。
下面是我目前为止用到的正则表达式,也就是ExcelPatternMsg
import java.util.regex.Pattern;
/**
* @author zhy
* @title: ExcelPatternMsg
* @projectName cec-moutai-bd-display
* @description: excel正则表达式,以及错误信息
* @date 2019/12/2614:22
*/
public class ExcelPatternMsg {
//只能输入整数或者小数
public static final String DECIMAL = "^[0-9]+\\.{0,1}[0-9]{0,2}$";
public static final String DECIMAL_MSG = "只能输入整数或者小数";
//日期格式 yyyy/MM/dd
public static final String DATE1 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\/(((0[13578]|1[02])\\/(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)\\/(0[1-9]|[12][0-9]|30))|(02\\/(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))\\/02\\/29)$";
public static final String DATE1_MSG = "输入正确的日期格式:yyyy/MM/dd";
//日期格式 yyyy-MM-dd
public static final String DATE2 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))-02-29)$";
public static final String DATE2_MSG = "输入正确的日期格式:yyyy-MM-dd";
//日期格式 yyyyMMdd
public static final String DATE3 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))0229)$";
public static final String DATE3_MSG = "输入正确的日期格式:yyyyMMdd";
//日期格式 yyyy-MM-dd HH:mm:ss
public static final String DATE_TIME1 = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" +
"((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
public static final String DATE_TIME1_MSG = "输入正确的日期格式:yyyy-MM-dd HH:mm:ss";
//日期格式 yyyy/MM/dd HH:mm:ss
public static final String DATE_TIME2 = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\/(((0[13578]|1[02])\\/(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)\\/(0[1-9]|[12][0-9]|30))|(02\\/(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))\\/02\\/29))\\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
public static final String DATE_TIME2_MSG = "输入正确的日期格式:yyyy/MM/dd HH:mm:ss";
//日期格式 yyyyMMddHHmmss
public static final String DATE_TIME3 = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))"+
"([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$";
public static final String DATE_TIME3_MSG = "输入正确的日期格式:yyyyMMddHHmmss";
//日期格式 yyyyMMddHHmmssSSS
public static final String DATE_TIME4 = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))0229))([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])([0-9]{3})$";
public static final String DATE_TIME4_MSG = "输入正确的日期格式:yyyyMMddHHmmssSSS";
//日期格式 yyyyMMdd HH:mm:ss
public static final String DATE_TIME5 = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))0229))\\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
public static final String DATE_TIME5_MSG = "输入正确的日期格式:yyyyMMdd HH:mm:ss";
//数字和字母
public static final String NUMBER_LETTER = "^[a-z0-9A-Z]+$";
public static final String NUMBER_LETTER_MSG = "只能输入数字和字母";
//数字
public static final String NUMBER = "^[0-9]*$";
public static final String NUMBER_MSG = "只能输入数字";
public static void main(String[] args) {
System.out.println(Pattern.matches(DATE1,"2020/02/30"));
}
}
- excel错误信息实体,继承excel数据实体、多了一个错误信息errMsg属性
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.zhy.easyexceldemo.easyexcel.ExcelPatternMsg;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
/**
* @title: UserExcelDto
* @projectName easyexceldemo
* @description: 用户excel类
* @author zhy
* @date 2020/1/1610:45
*/
@Data
@EqualsAndHashCode(callSuper=false)
public class UserExcelErrDto extends UserExcelDto {
//错误信息
@ExcelProperty(index = 4,value = "错误信息")
@ColumnWidth(50)
private String errMsg;
}
- UserService
import com.zhy.easyexceldemo.dto.UserExcelDto;
import com.zhy.easyexceldemo.easyexcel.ExcelCheckManager;
/**
* @title: UserService
* @projectName easyexceldemo
* @description: 用户service
* @author zhy
* @date 2020/1/1610:56
*/
public interface UserService extends ExcelCheckManager<UserExcelDto> {
}
- UserServiceImpl
import com.alibaba.excel.util.StringUtils;
import com.zhy.easyexceldemo.dto.UserExcelDto;
import com.zhy.easyexceldemo.easyexcel.ExcelCheckErrDto;
import com.zhy.easyexceldemo.easyexcel.ExcelCheckResult;
import com.zhy.easyexceldemo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @title: UserService
* @projectName easyexceldemo
* @description: 用户service
* @author zhy
* @date 2020/1/1610:56
*/
@Service
public class UserServiceImpl implements UserService {
//不合法名字
public static final String ERR_NAME = "史珍香";
/**
* @description: 校验方法
* @param userExcelDtos 用户信息
* @throws
* @return com.cec.moutai.common.easyexcel.ExcelCheckResult
* @author zhy
* @date 2019/12/24 14:57
*/
@Override
public ExcelCheckResult checkImportExcel(List<UserExcelDto> userExcelDtos) {
//成功结果集
List<UserExcelDto> successList = new ArrayList<>();
//错误数组
List<ExcelCheckErrDto<UserExcelDto>> errList = new ArrayList<>();
for (UserExcelDto userExcelDto : userExcelDtos) {
//错误信息
StringBuilder errMsg = new StringBuilder();
//根据自己的业务去做判断
if (ERR_NAME.equals(userExcelDto.getName()))
errMsg.append("请输入正确的名字").append(";");
if (StringUtils.isEmpty(errMsg.toString())){
//这里有两个选择,1、一个返回成功的对象信息,2、进行持久化操作
successList.add(userExcelDto);
}else{//添加错误信息
errList.add(new ExcelCheckErrDto(userExcelDto,errMsg.toString()));
}
}
return new ExcelCheckResult(successList,errList);
}
}
- web的导入导出
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson.JSON;
import com.zhy.easyexceldemo.common.BaseRest;
import com.zhy.easyexceldemo.common.Result;
import com.zhy.easyexceldemo.dto.UserExcelDto;
import com.zhy.easyexceldemo.dto.UserExcelErrDto;
import com.zhy.easyexceldemo.easyexcel.EasyExcelListener;
import com.zhy.easyexceldemo.easyexcel.EasyExcelUtils;
import com.zhy.easyexceldemo.easyexcel.ExcelCheckErrDto;
import com.zhy.easyexceldemo.pojo.User;
import com.zhy.easyexceldemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* @title: UserRest
* @projectName easyexceldemo
* @description: 用户rest
* @author zhy
* @date 2020/1/1611:30
*/
@RestController
@RequestMapping("/user")
public class UserRest extends BaseRest {
@Autowired
private UserService userService;
/**
* @description: 导出测试
* @param response
* @throws
* @return void
* @author zhy
* @date 2020/1/16 11:58
*/
@GetMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setName("张三");
user1.setAge(10);
user1.setBirthday(new Date());
user1.setSex("男");
userList.add(user1);
List<UserExcelDto> userExcelDtos = JSON.parseArray(JSON.toJSONString(userList),UserExcelDto.class);
EasyExcelUtils.webWriteExcel(response,userExcelDtos, UserExcelDto.class,"用户基本信息");
}
/**
* @description: 导入测试
* @param response
* @param file
* @throws
* @return com.zhy.easyexceldemo.common.Result
* @author zhy
* @date 2020/1/16 11:59
*/
@PostMapping("/importExcel")
public Result importExcel(HttpServletResponse response, @RequestParam MultipartFile file) throws IOException {
EasyExcelListener easyExcelListener = new EasyExcelListener(userService,UserExcelDto.class);
EasyExcelFactory.read(file.getInputStream(),UserExcelDto.class,easyExcelListener).sheet().doRead();
List<ExcelCheckErrDto<UserExcelDto>> errList = easyExcelListener.getErrList();
if (!errList.isEmpty()){//如果包含错误信息就导出错误信息
List<UserExcelErrDto> excelErrDtos = errList.stream().map(excelCheckErrDto -> {
UserExcelErrDto userExcelErrDto = JSON.parseObject(JSON.toJSONString(excelCheckErrDto.getT()), UserExcelErrDto.class);
userExcelErrDto.setErrMsg(excelCheckErrDto.getErrMsg());
return userExcelErrDto;
}).collect(Collectors.toList());
EasyExcelUtils.webWriteExcel(response,excelErrDtos, UserExcelErrDto.class,"用户导入错误信息");
}
return addSucResult();
}
}
最终的结果是:
转载请注明出处!
本人为(weixin)恭(gong)祝(zhong)号:一吱小确幸。欢迎大家关注