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;
}
相关推荐
程序猿chen3 分钟前
《Java八股文の文艺复兴》第十篇:量子永生架构——对象池的混沌边缘
java·后端·面试·架构·跳槽·量子计算·改行学it
了不起的杰8 分钟前
【Linux操作系统】:信号
linux·运维·服务器
沐土Arvin13 分钟前
探索原生JS的力量:自定义实现类似于React的useState功能
前端·javascript·react.js
WEI_Gaot16 分钟前
HTML列表
前端·html
WEI_Gaot17 分钟前
HTML表单
前端·html
blog_jenny24 分钟前
Android 14 、15动态申请读写权限实现 (Java)
android·java·gitee
顾林海25 分钟前
Flutter容器组件深度解析
android·前端·flutter
zooKevin27 分钟前
一个很好用的vue2在线签名组件
前端
onejason27 分钟前
如何解析商品的价格信息?
前端·后端·php
用户5669994370332 分钟前
一篇文章弄懂Lambda 表达式
java