EasyPoi Excel 注解版 导入导出 枚举或者字典值 replace 动态处理

示例代码,需要将 replace 的枚举值动态处理

/**
     * 订单类型
     */
    @ApiModelProperty(value = "订单类型")
    @Dict(dictCode = "ORDER_TYPE")
    @Excel(name = "订单类型" ,width = 15,replace = {"_null","线上直销_1","代理商散客_2","代理商团队_3","大客户_4","代客下单_5","线下_6"})
    private String orderType;

这里我们可以在service层用一个切面处理

/**
 * @author 作者:xujuncheng
 * @date 时间:2021/9/16 - 11:00
 * @email 邮箱:18475346620@163.com
 * @desc 描述:Excel数据字典切面
 */
@Aspect
@Component
@Slf4j
public class ExcelAspect {

    @Autowired
    private RedisTemplate redisTemplate;

    
    private static final String DATA_SYS="Dict-data";


    /**
     * 切点,切入 controller 包下面的所有方法
     */
    @Pointcut("execution(public * com.text.finance.service..*.*ServiceImpl.export*(..))")
    public void dict() {

    }

    @Around("dict()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long time1 = System.currentTimeMillis();
        Object result = pjp.proceed();
        long time2 = System.currentTimeMillis();
        log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
        long start = System.currentTimeMillis();
        this.parseExcelText(result);
        long end = System.currentTimeMillis();
        log.debug("解析注入JSON数据  耗时" + (end - start) + "ms");
        return result;
    }
    private void parseExcelText(Object result) {
        Object o = redisTemplate.opsForValue().get(DATA_SYS).toString();
        List<SysDataDictionaryRedisDTO> object = JSON.parseArray(o.toString(),SysDataDictionaryRedisDTO.class);
        List<?> list = (List<?>) result;
        if (list != null && list.size() > 0){
            for (Object record : list) {
                for (Field allField : ObjConvertUtils.getAllFields(record)) {
                    Excel annotation = allField.getAnnotation(Excel.class);
                    Dict dict = allField.getAnnotation(Dict.class);
                    if (annotation != null  && dict != null) {
                        //获取字段code
                        try {
                            InvocationHandler dictInvocationHandler = Proxy.getInvocationHandler(dict);
                            Field dictMemberValues = dictInvocationHandler.getClass().getDeclaredField("memberValues");
                            dictMemberValues.setAccessible(true);
                            Map dictMap = (Map) dictMemberValues.get(dictInvocationHandler);
                            String dictCode = (String) dictMap.get("dictCode");
                            List<SysDataDictionaryRedisDTO> collect = object.stream().filter(item -> item.getDictCode().equals(dictCode)).collect(Collectors.toList());
                            if(CollectionUtil.isNotEmpty(collect)){
                                List<String> strings = new ArrayList<>();
                                strings.add("_null");
                                collect.forEach(item -> {
                                    String dictName = item.getDictName();
                                    String dictVal = item.getDictVal();
                                    strings.add(dictName+"_"+dictVal);
                                });
                                String[] strings1 = strings.toArray(new String[0]);
                                InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                                Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
                                memberValues.setAccessible(true);
                                Map map = (Map) memberValues.get(invocationHandler);
                                map.put("replace", strings1);
                            }
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

跟简单,轻量级的处理方式
直接用反射处理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值