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;
}
相关推荐
yaoxin5211231 分钟前
80. Java 枚举类 - 使用枚举实现单例模式
java·开发语言·单例模式
Clownseven8 分钟前
[深度解析] 服务器内存(RAM)演进之路(2025):DDR5 vs HBM vs CXL 内存技术与选型指南
运维·服务器
2401_8960081924 分钟前
TCP连接状态说明
运维·服务器·网络
夏季疯27 分钟前
学习笔记:黑马程序员JavaWeb开发教程(2025.4.7)
java·笔记·学习
鸡吃丸子1 小时前
常见的实时通信技术(轮询、sse、websocket、webhooks)
前端·websocket·状态模式
卡戎-caryon1 小时前
【C++】15.并发支持库
java·linux·开发语言·c++·多线程
胡斌附体1 小时前
vue添加loading后修复页面渲染问题
前端·javascript·vue.js·渲染·v-if·异步加载
weixin_434255611 小时前
命令行快速上传文件到SFTP服务器(附参考示例)
linux·运维·服务器
头发那是一根不剩了2 小时前
怎么用idea分析hprof文件定位JVM内存问题
java·jvm
掘金-我是哪吒2 小时前
分布式微服务系统架构第133集:运维服务器6年经验,高并发,大数据量系统
运维·服务器·分布式·微服务·系统架构