Java 使用easyExcel 动态设置列名

1.导出类


import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import lombok.Data;
@Data
@HeadFontStyle(fontHeightInPoints = 14)
@ExcelIgnoreUnannotated
public class bb {
        @ExcelProperty("备注1")
        private String comment1;

        @ExcelProperty("备注2")
        private String comment2;

        @ExcelProperty("备注3")
        private String comment3;

        @ExcelProperty("备注4")
        private String comment4;

        @ExcelProperty("备注5")
        private String comment5;
}

2.利用反射,修改ExcelProperty 的value字段



import com.alibaba.excel.annotation.ExcelProperty;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

public class aa {
    public static void main(String[] args) {
        getLogExportClass();
    }

    private static Class getLogExportClass() {
        try {
            Class srcClass = bb.class;
            try {
                Field field = null;
                field = srcClass.getDeclaredField("comment1");
                ExcelProperty excel = field.getAnnotation(ExcelProperty.class);
                InvocationHandler excelH = Proxy.getInvocationHandler(excel);
                Field excelF = excelH.getClass().getDeclaredField("memberValues");
                excelF.setAccessible(true);
                Map excelValues = (Map) excelF.get(excelH);
                excelValues.put("value", new String[]{"替换后的字段"});
            } catch (Exception e) {
                e.printStackTrace();
            }
            return srcClass;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值