easyexcel 动态列导出

  1. 引入easyexcel
XML 复制代码
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.2.1</version>
        </dependency>

2.导出write

java 复制代码
public void export(HttpServletResponse response) {
        try {
            String fileName = "测试导出动态列" + System.currentTimeMillis();

            Set<String> includeColumnFiledNames = new HashSet<>();
            includeColumnFiledNames.add("title");
            includeColumnFiledNames.add("openId");
            List<Category> categoryList = getAllCategory();
            List<CategoryExport> categoryExports = BeanUtil.copyToList(categoryList, CategoryExport.class);
            response.setContentType("application/vnd.ms-excel");// 设置文本内省
            response.setCharacterEncoding("utf-8");// 设置字符编码
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); // 设置响应头
            ExcelWriterBuilder write = EasyExcel.write(response.getOutputStream(), CategoryExport.class);
            if (includeColumnFiledNames.size() > 0) {
                write.includeColumnFieldNames(includeColumnFiledNames);
            }
            write.sheet("模板").doWrite(categoryExports); //用io流来写入数据
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.导出类

java 复制代码
@Data
public class CategoryExport implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    @ExcelProperty("openId")
    private String openId;
    @ExcelProperty("priority")
    private Integer priority;
    @ExcelProperty("标题")
    private String title;
}
相关推荐
海宇数据9 分钟前
零信任架构实战:基于海宇企业股权变更构建自动化投研异动监测网关
java·人工智能·架构·自动化
AI技术新视界11 分钟前
Qwen4-35B-A3B 已发布,社区第一波实测数据已出炉
服务器·人工智能·llm
一铭1119920 分钟前
微信小程序可视化搭建实战:模板、组件与发布验收
前端·低代码·微信小程序·小程序·页面设计·可视化搭建·发布验收
旋风菠萝36 分钟前
实现多线程的方法
java
LabVIEW开发41 分钟前
LabVIEW 2025 新特性盘点:这几个改进,值得你认真看一遍
前端·labview
Pin_Qing1 小时前
当旗舰模型挂上 Flash 的价签:MiniMax M4 的定价逻辑与工程账本
服务器·开发语言·网络·人工智能·程序人生
Huangjin007_1 小时前
【Linux 系统篇(四十四)】线程(五):线程互斥:从共享资源到互斥锁
linux·运维·服务器
Thanks_ks1 小时前
单例模式与工厂模式精讲:设计模式的意图、实现与在 Java 项目中的实际应用
java·spring·单例模式·设计模式·工厂模式·线程安全·面向对象设计
xiangyun611 小时前
Ubuntu 网络排障决策树(5):SYN 到了网卡,为什么既没有 SYN-ACK 也没有 RST
linux·运维·服务器·网络·安全·ubuntu
CoderYanger1 小时前
Java EE 进阶:16. Spring原理(第二弹)
java·服务器·spring boot·后端·spring·面试·java-ee