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;
}
相关推荐
原来是好奇心5 分钟前
Spring Boot缓存实战:@Cacheable注解详解与性能优化
java·spring·mybatis·springboot
qq_321665336 分钟前
验证centos 服务器(或其他linux服务器),443或80端口是否开通
linux·服务器·centos
java_logo6 分钟前
TOMCAT Docker 容器化部署指南
java·linux·运维·docker·容器·tomcat
麦克马7 分钟前
Netty和Tomcat有什么区别
java·tomcat
程序员小假14 分钟前
SQL 语句左连接右连接内连接如何使用,区别是什么?
java·后端
怕什么真理无穷16 分钟前
C++_面试题_21_字符串操作
java·开发语言·c++
Fantasydg16 分钟前
Request Response对象
前端
Wect17 分钟前
学习React-DnD:核心组件与Hooks
前端
humors22123 分钟前
前端开发案例(不定期更新)
前端·vue.js·elementui·ruoyi·若依
菠萝+冰25 分钟前
npm中-d -g 和默认安装的区别
前端·npm·node.js