¶Ô±ÈÐÂÎļþ |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |