| | |
| | | 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.JsonUtils; |
| | | import com.ruoyi.common.utils.StreamUtils; |
| | | import com.ruoyi.common.utils.ValidatorUtils; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.ConstraintViolationException; |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * Excel 导入监听 |
| | |
| | | * 是否Validator检验,默认为是 |
| | | */ |
| | | private Boolean isValidate = Boolean.TRUE; |
| | | |
| | | /** |
| | | * excel 表头数据 |
| | | */ |
| | | private Map<Integer, String> headMap; |
| | | |
| | | /** |
| | | * 导入回执 |
| | |
| | | String errMsg = null; |
| | | if (exception instanceof ExcelDataConvertException) { |
| | | // 如果是某一个单元格的转换异常 能获取到具体行号 |
| | | // 如果要获取头的信息 配合doAfterAllAnalysedHeadMap使用 |
| | | ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception; |
| | | errMsg = StrUtil.format("第{}行-第{}列解析异常<br/>", |
| | | excelDataConvertException.getRowIndex() + 1, |
| | | excelDataConvertException.getColumnIndex() + 1); |
| | | Integer rowIndex = excelDataConvertException.getRowIndex(); |
| | | Integer columnIndex = excelDataConvertException.getColumnIndex(); |
| | | errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", |
| | | rowIndex + 1, columnIndex + 1, headMap.get(columnIndex)); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | | } |
| | |
| | | if (exception instanceof ConstraintViolationException) { |
| | | ConstraintViolationException constraintViolationException = (ConstraintViolationException) exception; |
| | | Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations(); |
| | | String constraintViolationsMsg = constraintViolations.stream() |
| | | .map(ConstraintViolation::getMessage) |
| | | .collect(Collectors.joining(", ")); |
| | | String constraintViolationsMsg = StreamUtils.join(constraintViolations, ConstraintViolation::getMessage, ", "); |
| | | errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | |
| | | |
| | | @Override |
| | | public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
| | | log.debug("解析到一条头数据: {}", JSON.toJSONString(headMap)); |
| | | this.headMap = headMap; |
| | | log.debug("解析到一条表头数据: {}", JsonUtils.toJsonString(headMap)); |
| | | } |
| | | |
| | | @Override |