easyexcel 大佬们看看这个能实现吗

ix0qys7i  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(48)

各位大佬,我有个需求就是我想在实体类上加个List,A里面有个year年份,有个年份对应的属性获奖award.如何让他们的值分别对应作为表头和表值啊

bxjv4tth

bxjv4tth1#

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // 创建数据列表
        List<AwardData> dataList = new ArrayList<>();
        dataList.add(new AwardData("2021", "Best Actor"));
        dataList.add(new AwardData("2022", "Best Director"));
        dataList.add(new AwardData("2023", "Best Picture"));

        // 写入 Excel 文件
        EasyExcel.write("awards.xlsx", AwardData.class)
                .sheet("Awards")
                .doWrite(dataList);
    }

    // 实体类
    public static class AwardData {
        @ExcelProperty("Year")
        private String year;

        @ExcelProperty("Award")
        private String award;

        // 构造函数、getter 和 setter 方法省略

        public AwardData(String year, String award) {
            this.year = year;
            this.award = award;
        }
    }
}

相关问题