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;
}
相关推荐
憧憬blog29 分钟前
【Kiro开发集训营】拒绝“屎山”堆积:在 Kiro 中重构“需求-代码”的血缘关系
java·开发语言·kiro
e***74951 小时前
Spring Security 官网文档学习
java·学习·spring
行云流水6261 小时前
uniapp pinia实现数据持久化插件
前端·javascript·uni-app
n***i951 小时前
Java NIO文件操作
java·开发语言·nio
zhangyao9403302 小时前
uniapp动态修改 顶部导航栏标题和右侧按钮权限显示隐藏
前端·javascript·uni-app
笃行客从不躺平2 小时前
接口幂等性(Idempotency)
java
Hero | 柒3 小时前
JAVA反射机制
java·spring·反射
hanyi_qwe3 小时前
文本三剑客--awk
linux·运维·服务器
j***63083 小时前
Springboot项目中线程池使用整理
java·spring boot·后端
likuolei3 小时前
Eclipse 创建 Java 接口
java·数据库·eclipse