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;
}
相关推荐
^yi12 分钟前
【Linux系统编程】库的制作与使用
linux·运维·服务器
geovindu14 分钟前
java: Gale-Shapley Algorithm
java·开发语言·后端·算法
打妖妖灵滴哪吒20 分钟前
IOS模态窗口的作用与设计原则
前端·ui
LUCKY-LIVING27 分钟前
ELF File in linux
android·java·linux
午安~婉28 分钟前
挑战二:博客预览卡片
前端·javascript·html
bksczm39 分钟前
popen解析
linux·运维·服务器
环境栈笔记44 分钟前
指纹浏览器安全评测方法:核对环境、数据与权限后再选型
前端·人工智能·后端·自动化
深念Y1 小时前
技术探索记录 在 Android 手机上运行 One API
android·linux·服务器·智能手机·go·交叉编译·服务
星核0penstarry1 小时前
DeepSeek-V4-Flash 正式公测:大模型行业进入「极速平价普惠时代」
java·开发语言·人工智能
打妖妖灵滴哪吒1 小时前
前端开发/UI设计师-APP中如何进行可发现性设计
前端·ui