add 新增导入支持开启Validator数据验证,支持开启导入异常行继续读取,支持返回导入回执
This commit is contained in:
parent
d2b7f8ef0f
commit
c3bb4d2a61
@ -0,0 +1,116 @@
|
||||
package com.ruoyi.common.utils.poi;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import com.alibaba.excel.exception.ExcelAnalysisException;
|
||||
import com.alibaba.excel.exception.ExcelDataConvertException;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.ruoyi.common.utils.ValidatorUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import javax.validation.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 公共excel监听类
|
||||
* @param <T>
|
||||
*/
|
||||
public class ExcelListener<T> extends AnalysisEventListener<T> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelListener.class);
|
||||
/** 数据对象list */
|
||||
private final List<T> list = new ArrayList<>();
|
||||
/** 错误信息列表 */
|
||||
private final List<String> errorList = new ArrayList<>();
|
||||
/** 遇到异常是否跳出导入,默认为是 */
|
||||
private Boolean skipException = Boolean.TRUE;
|
||||
/** 是否Validator检验,默认为是 */
|
||||
private Boolean isValidate = Boolean.TRUE;
|
||||
/**
|
||||
* 导入回执
|
||||
*/
|
||||
private final ExcelResult<T> excelResult = new ExcelResult<>();
|
||||
|
||||
public ExcelListener() {
|
||||
|
||||
}
|
||||
|
||||
public ExcelListener(boolean isValidate, boolean skipException) {
|
||||
this.isValidate = isValidate;
|
||||
this.skipException = skipException;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理异常
|
||||
*
|
||||
* @param exception ExcelDataConvertException
|
||||
* @param context excel上下文
|
||||
*/
|
||||
@Override
|
||||
public void onException(Exception exception, AnalysisContext context) throws Exception {
|
||||
// 如果是某一个单元格的转换异常 能获取到具体行号
|
||||
// 如果要获取头的信息 配合doAfterAllAnalysedHeadMap使用
|
||||
String errMsg = null;
|
||||
if (exception instanceof ExcelDataConvertException) {
|
||||
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
|
||||
errMsg = StrUtil.format("第{}行-第{}列解析异常<br/>", excelDataConvertException.getRowIndex() + 1,
|
||||
excelDataConvertException.getColumnIndex() + 1);
|
||||
LOGGER.error(errMsg);
|
||||
}
|
||||
if (exception instanceof ConstraintViolationException) {
|
||||
ConstraintViolationException constraintViolationException = (ConstraintViolationException)exception;
|
||||
Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
|
||||
String constraintViolationsMsg= CollUtil.join(constraintViolations
|
||||
.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toList()),
|
||||
",");
|
||||
errMsg = StrUtil.format("第{}行数据校验异常:{}", context.readRowHolder().getRowIndex() + 1,
|
||||
constraintViolationsMsg);
|
||||
LOGGER.error(errMsg);
|
||||
}
|
||||
errorList.add(errMsg);
|
||||
if (!skipException){
|
||||
throw new ExcelAnalysisException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||
LOGGER.debug("解析到一条头数据:{}", JSON.toJSONString(headMap));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(T data, AnalysisContext context) {
|
||||
if (isValidate) {
|
||||
ValidatorUtils.validate(data);
|
||||
}
|
||||
list.add(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
excelResult.setList(list);
|
||||
excelResult.setErrorList(errorList);
|
||||
LOGGER.debug("所有数据解析完成!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导入数据
|
||||
* @return 导入数据
|
||||
*/
|
||||
public List<T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public ExcelResult<T> getExcelResult() {
|
||||
return excelResult;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.ruoyi.common.utils.poi;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ExcelResult<T> {
|
||||
|
||||
/** 数据对象list
|
||||
*/
|
||||
private List<T> list;
|
||||
/** 错误信息列表 */
|
||||
private List<String> errorList;
|
||||
|
||||
/**
|
||||
* 获取导入回执
|
||||
* @return 导入回执
|
||||
*/
|
||||
public String getAnalysis() {
|
||||
int successCount = list.size();
|
||||
int errorCount = errorList.size();
|
||||
if (successCount == 0) {
|
||||
return "读取失败,未解析到数据";
|
||||
} else {
|
||||
if (errorList.size() == 0) {
|
||||
return StrUtil.format("恭喜您,全部读取成功!共{}条", successCount);
|
||||
} else {
|
||||
return StrUtil.format("部分读取成功,其中成功{}条,失败{}条,错误信息如下:<br/>{}",
|
||||
successCount,
|
||||
errorCount,
|
||||
CollUtil.join(errorList, "<br/>"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy
|
||||
import com.ruoyi.common.convert.ExcelBigNumberConvert;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -30,6 +31,19 @@ public class ExcelUtil {
|
||||
return EasyExcel.read(is).head(clazz).autoCloseStream(false).sheet().doReadSync();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对excel表单默认第一个索引名转换成list(EasyExcel)
|
||||
*
|
||||
* @param is 输入流
|
||||
* @return 转换后集合
|
||||
*/
|
||||
public static <T> ExcelResult<T> importExcel(InputStream is, Class<T> clazz, boolean isValidate, boolean skipException) {
|
||||
ExcelListener<T> listener = new ExcelListener<>(isValidate, skipException);
|
||||
EasyExcel.read(is, clazz, listener).sheet().doRead();
|
||||
return listener.getExcelResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对list数据源将其里面的数据导入到excel表单(EasyExcel)
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user