easyPoi 导入时自定义校验和字典value转换字典key

实体类

@Data
public class User implements IExcelDataModel, IExcelModel {
    // TODO: 这里的rowNum和errorMsg可以自动填充值

    /**
     * 行号
     */
    private int rowNum;

    /**
     * 错误消息
     */
    private String errorMsg;

    @Excel(name = "姓名")
    @NotBlank(message = "姓名不能为空")
    private String name;

    @Excel(name = "性别")
    @Pattern(regexp = "[01]", message = "性别错误") // 使用正则必须是String
    private String sex;

    @Excel(name = "地址")
    private String address;

    @Excel(name = "城市")
    private String cityName;
}

自定义校验

@Component
public class ExcelImportHandler implements IExcelVerifyHandler<User> {


    @Override
    public ExcelVerifyHandlerResult verifyHandler(User user) {
        StringJoiner joiner = new StringJoiner(",");
        String name = user.getName();
        String sex = user.getSex();
        if (name == null || "".equals(name)) {
            joiner.add("姓名不能为空");
        }
        if (!"女".equals(sex) || !"男".equals(sex)) {
            joiner.add("性别不合法");
        }

        // 其他校验
        if () {
        
        }

        if (joiner.length() > 1) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }
}

  这里是controller,在每次导入的时候进行了一次查询数据库,动态设置@replace的操作

    @Resource
    ExcelImportHandler excelImportHandler;

    @PostMapping("/excelImport")
    public Object excelImport(MultipartFile file) throws Exception {

        // 定义所有需要查询的字典值列表,key为导入对象的属性名,value为字典表dict_type
        HashMap<String, String> dictMap = new HashMap<>();
        dictMap.put("sex", "sys_user_sex");
        // 自由添加


        Field[] fields = User.class.getDeclaredFields();
        
        try {
            for (Field field : fields) {
                // todo 是需要转化的属性就查找对应的字典列表,并填充进@Excel的replace
                if (dictMap.containsKey(field.getName())) {
                    Excel annotation = field.getAnnotation(Excel.class);
                    InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                    Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
                    memberValues.setAccessible(true);
                    // todo 查询字典列表,动态生成values,下面假定为数据库查询出的字典列表构造成的values


                    String[] values = new String[]{"男_1", "女_0"};
                    Map map = (Map) memberValues.get(invocationHandler);
                    map.put("replace", values);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1); 
        params.setNeedVerify(true); // 开启@NotNull这些校验
        // params.setVerifyHandler(excelImportHandler); // 这里是自定义的校验类,需要用到就打开
        // 但本人还不清楚怎么在自定义校验类中校验加了@replace的属性,测试中发现无论是汉字男、女还是转换后的0,1都不能通过校验
        // 所以我的使用习惯就是直接在实体类中使用正则校验,不使用自定义校验
        // 知道的猿猿留个言呀

        ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file.getInputStream(), User.class, params);


        System.out.println("校验失败的集合:" + result.getFailList());
        System.out.println("校验通过的集合:" + result.getList());


        return result;

    }

 我的excel数据:

 

 打印出的通过和未通过校验数据:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值