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;
}
相关推荐
A星空1231 分钟前
三、Kconfig介绍以及制作menuconfig界面
linux·运维·服务器
问道飞鱼2 分钟前
【前端知识】Vite用法从入门到实战
前端·vite·项目构建
爱上妖精的尾巴7 分钟前
8-10 WPS JSA 正则表达式:贪婪匹配
服务器·前端·javascript·正则表达式·wps·jsa
常利兵13 分钟前
吃透Java操作符高阶:位操作符+赋值操作符全解析(Java&C区别+实战技巧+面试考点)
java·c语言·面试
不用89k20 分钟前
SpringBoot学习新手项初识请求
java·spring boot·学习
码农阿豪42 分钟前
SpringBoot实现公正有趣好玩的年会抽奖系统
java·spring boot·后端
Java爱好狂.42 分钟前
RDB&AOF持久化原理解析
java·数据库·redis·后端开发·java编程·java程序员·java八股文
hashiqimiya1 小时前
gradle.properties使用系统代理
java
Aliex_git1 小时前
浏览器 API 兼容性解决方案
前端·笔记·学习
独泪了无痕1 小时前
useStorage:本地数据持久化利器
前端·vue.js