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
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
EasyExcel中,可以通过使用注解来实现动态列名的导出。具体步骤如下: 1. 创建一个实体类,例如User,用于封装每一行的数据。 2. 在实体类中,使用注解@ExcelProperty来标记每个属性对应的列名。可以使用字符串数组来表示动态列名,例如@ExcelProperty({"列名1", "列名2"})。 3. 在导出时,根据需要展示的列的信息,动态生成列名数组,并将其设置到实体类的注解中。 4. 使用ExcelWriter对象的write方法将表头和数据写入Excel文件。 以下是一个示例代码: ```java public class User { @ExcelProperty({"姓名", "Name"}) private String name; @ExcelProperty({"年龄", "Age"}) private int age; // 其他属性... // getter和setter方法... } public class ExportUtil { public static void exportData(List<User> userList, List<String> columnNames, String filePath) { // 创建ExcelWriter对象 ExcelWriter excelWriter = EasyExcel.write(filePath).build(); // 创建Sheet对象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 设置动态列名 List<List<String>> head = new ArrayList<>(); head.add(columnNames); writeSheet.setHead(head); // 写入数据 excelWriter.write(userList, writeSheet); // 关闭ExcelWriter对象 excelWriter.finish(); } } ``` 使用示例: ```java List<User> userList = new ArrayList<>(); // 添加用户数据... List<String> columnNames = new ArrayList<>(); // 添加动态列名... String filePath = "导出文件路径"; ExportUtil.exportData(userList, columnNames, filePath); ``` 这样就可以实现EasyExcel导出动态列名的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值