Java EasyExcel动态修改注解

Java EasyExcel动态修改注解

ASelince


实现:

动态修改注解工具类:
    public void modifyFiled(Class clazz, Consumer<Map> consumer, String fieldName) throws Exception {
        //获取需要修改的属性
        Field field = clazz.getDeclaredField(fieldName);
        //获取注解
        ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
        //生成代理类对象
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        Field annotationValues = invocationHandler.getClass().getDeclaredField("memberValues");

        annotationValues.setAccessible(true);
        Map map = (Map) annotationValues.get(invocationHandler);
        consumer.accept(map);
    }

导出Excel实体类:
@Data
@ColumnWidth(16)
@HeadRowHeight(57)
public class FiveAchievementScoreExcel implements Serializable {

    private static final long serialVersionUID = 1L;

    @ExcelProperty(index = 0, value = {"{%s}", "系统编号"})
    private String id;

    @ColumnWidth(7)
    @ExcelProperty(index = 1, value = {"{%s}", "序号"})
    private Integer reportNo;

    @ColumnWidth(36)
    @ExcelProperty(index = 2, value = {"{%s}", "项目名称"})
    @ApiModelProperty(value = "成果名称")
    private String achievementName;

    @ExcelProperty(index = 3, value = {"{%s}", "推荐值"})
    private Integer recommendIndex;
}


业务实现
    public void reBuildExcelPropertyValue(Class<?> clazz, String title) throws Exception {

        Field[] exportFields = clazz.getDeclaredFields();
        for (Field field : exportFields) {

            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            if (annotation != null) {

                String[] value = annotation.value();

                if (StringUtils.startsWith(value[0], "{") && StringUtils.endsWith(value[0], "}")) {

                    modifyFiled(FiveAchievementScoreExcel.class, map -> {
                        map.put("value",
                                new String[]{StringUtils.replaceEach(String.format(value[0], title),
                                        new String[]{"{", "}"},
                                        new String[]{"", ""}),
                                        value[1]});
                    }, field.getName());
                }
            }
        }
    }


代码调用
 excelUtil.reBuildExcelPropertyValue(FiveAchievementScoreExcel.class, activityName);
 

private static void replaceExcelPropertyValue(Class<?> clazz, String fieldName, String fieldValue) {
    //获取field
    Field filed = null;
    try {
        filed = clazz.getDeclaredField(fieldName);
        filed.setAccessible(true);
        //获取注解
        ExcelProperty annotation = filed.getAnnotation(ExcelProperty.class);

        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
        memberValues.setAccessible(true);
        Map<String, Object> map = (Map<String, Object>) memberValues.get(invocationHandler);
        String[] valueArr = {fieldValue};
        map.put("value", valueArr);
        System.out.println(JSON.toJSONString(map.get("value")));
    } catch (Exception e) {
        System.out.println(e.getMessage() + "  "  + e);
    }
}

public static void main(String[] args) {
    Class<AftersaleExcel> aftersaleExcelClass = AftersaleExcel.class;
    //获取field
    Field filed = null;
    String fieldName = "orderSourceStr";
    try {
        filed = aftersaleExcelClass.getDeclaredField(fieldName);
        System.out.println(filed.getName());

        filed.setAccessible(true);
        //获取注解
        ExcelProperty annotation = filed.getAnnotation(ExcelProperty.class);

        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        System.out.println(invocationHandler.getClass().getName());
        Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
        System.out.println(memberValues.getName() + "------------------");
        memberValues.setAccessible(true);
        Map<String, Object> map = (Map<String, Object>) memberValues.get(invocationHandler);
        String[] valueArr = {"oldvalue001"};
        map.put("value", valueArr);
        System.out.println(JSON.toJSONString(map.get("value")));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

     replaceExcelPropertyValue(aftersaleExcelClass, fieldName, "newValue001");

    try {
        filed = aftersaleExcelClass.getDeclaredField(fieldName);
        filed.setAccessible(true);

        //获取注解
        ExcelProperty annotation = filed.getAnnotation(ExcelProperty.class);

        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
        memberValues.setAccessible(true);
        Map<String, Object> map = (Map<String, Object>) memberValues.get(invocationHandler);

        System.out.println(JSON.toJSONString(map.get("value")));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

猜你喜欢

转载自blog.csdn.net/zxl2016/article/details/131520571