EasyExcel导入,如何校验导入的数据(例如:不能为空。)?

本文介绍如何使用Java注解实现Excel数据导入时的字段有效性校验,包括必填字段标记、自定义异常类及在分析监听器中的应用。通过实例演示了如何创建ExcelValid注解、检查字段值并抛出ExceptionCustom异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注解的方式校验

1.声明不能为空校验注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>Excel导入必填校验注解</p>
 *
 */
@Target({ ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelValid {
    String message() default "导入有未填入的字段";
}

2.excel模板对象相应属性添加注解
在这里插入图片描述
3.编写字段校验类(类似监听器的作用)
ExceptionCustom为继承RuntimeException 的自定义异常类

import com.mique.annotation.ExcelValid;
import com.mique.utils.exception.ExceptionCustom;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * <p>Excel导入字段校验</p>
 *
 * created By DoLaLi on 2022/3/4
 */
public class ExcelImportValid {
    /**
     * Excel导入字段校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     */
    public static void valid(Object object) throws ExceptionCustom{
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            try {
                fieldValue = field.get(object);
            } catch (IllegalAccessException e) {
                throw new ExceptionCustom("IMPORT_PARAM_CHECK_FAIL", "导入参数检查失败");
            }
            //是否包含必填校验注解
            boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
            if (isExcelValid && Objects.isNull(fieldValue)) {
                throw new ExceptionCustom("NULL", field.getAnnotation(ExcelValid.class).message());
            }

        }
    }
}

4.在继承AnalysisEventListener类的public void invoke(PatientExcelModel data, AnalysisContext analysisContext)方法中加校验

    @Override
    public void invoke(PatientExcelModel patientExcelModel, AnalysisContext analysisContext){

        try {
            //通用方法数据校验
            ExcelImportValid.valid(patientExcelModel);
        }catch (ExceptionCustom e){
            System.out.println(e.getMessage());
            //在easyExcel监听器中抛出业务异常
            throw new ExcelAnalysisException(e.getMessage());
        }
        //TODO 业务代码
   }

ExceptionCustom 类

package com.xxx.utils.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 自定义注解异常
 * 
 * @author DoLaLi
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ExceptionCustom extends RuntimeException
{
    private static final long serialVersionUID = 1L;

    public ExceptionCustom()
    {
    }

    /**
     * 错误编码
     */
    private String errorCode;

    /**
     * 消息是否为属性文件中的Key
     */
    private boolean propertiesKey = true;

    /**
     * 构造一个基本异常.
     *
     * @param message
     *            信息描述
     */
    public ExceptionCustom(String message)
    {
        super(message);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode
     *            错误编码
     * @param message
     *            信息描述
     */
    public ExceptionCustom(String errorCode, String message)
    {
        this(errorCode, message, true);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode
     *            错误编码
     * @param message
     *            信息描述
     */
    public ExceptionCustom(String errorCode, String message, Throwable cause)
    {
        this(errorCode, message, cause, true);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode
     *            错误编码
     * @param message
     *            信息描述
     * @param propertiesKey
     *            消息是否为属性文件中的Key
     */
    private ExceptionCustom(String errorCode, String message, boolean propertiesKey)
    {
        super(message);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode
     *            错误编码
     * @param message
     *            信息描述
     */
    public ExceptionCustom(String errorCode, String message, Throwable cause, boolean propertiesKey)
    {
        super(message, cause);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 构造一个基本异常.
     *
     * @param message
     *            信息描述
     * @param cause
     *            根异常类(可以存入任何异常)
     */
    public ExceptionCustom(String message, Throwable cause)
    {
        super(message, cause);
    }

}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值