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;
}
相关推荐
消失的旧时光-19439 小时前
第十四课:Redis 在后端到底扮演什么角色?——缓存模型全景图
java·redis·缓存
BD_Marathon9 小时前
设计模式——依赖倒转原则
java·开发语言·设计模式
鹏北海9 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
BD_Marathon9 小时前
设计模式——里氏替换原则
java·设计模式·里氏替换原则
发现一只大呆瓜9 小时前
虚拟列表:从定高到动态高度的 Vue 3 & React 满分实现
前端·vue.js·react.js
Coder_Boy_9 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例中相关概念
java·人工智能·spring boot·后端·spring
css趣多多9 小时前
add组件增删改的表单处理
java·服务器·前端
雨中飘荡的记忆9 小时前
Spring Batch实战
java·spring
证榜样呀9 小时前
2026 大专计算机专业必考证书推荐什么
大数据·前端
Sheep Shaun9 小时前
揭开Linux的隐藏约定:你的第一个文件描述符为什么是3?
linux·服务器·ubuntu·文件系统·缓冲区