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;
}
相关推荐
云烟成雨TD10 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
lwx91485210 小时前
Linux-Shell算术运算
linux·运维·服务器
于慨10 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz10 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
swg32132110 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
从前慢丶10 小时前
前端交互规范(Web 端)
前端
gelald10 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川11 小时前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
一轮弯弯的明月11 小时前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
CHU72903511 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序