Search in sources :

Example 11 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        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(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 12 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project RuoYi-Cloud-Plus by JavaLionLi.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        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(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)12 ConstraintViolation (javax.validation.ConstraintViolation)4 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)3 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)3 ConstraintViolationException (javax.validation.ConstraintViolationException)3 Head (com.alibaba.excel.metadata.Head)2 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)2 Map (java.util.Map)2 EasyExcel (com.alibaba.excel.EasyExcel)1 ExcelWriter (com.alibaba.excel.ExcelWriter)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)1 ReadConverterContext (com.alibaba.excel.converters.ReadConverterContext)1 ModelBuildEventListener (com.alibaba.excel.read.listener.ModelBuildEventListener)1 ReadListener (com.alibaba.excel.read.listener.ReadListener)1 ReadSheetHolder (com.alibaba.excel.read.metadata.holder.ReadSheetHolder)1 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1 BindDict (com.diboot.core.binding.annotation.BindDict)1 BaseConfig (com.diboot.core.config.BaseConfig)1 BusinessException (com.diboot.core.exception.BusinessException)1