Java中使用alibaba的easyexcel中的方法实现csv模板下载功能

系列文章目录

文章目录


一、EasyExcelUtil工具

java 复制代码
 /**
     * @param response     响应
     * @param fileName     文件名称
     * @param sheetName    sheet名称
     * @param headNameList 头部名称
     * @param <T>
     * @throws IOException
     */
    public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<String> headNameList) throws IOException {
        OutputStream out = null;
        try {
//            response.setContentType("application/vnd.ms-excel");
            response.setContentType("text/csv");
            response.setCharacterEncoding(CharEncoding.UTF_8);
            fileName = URLEncoder.encode(fileName, CharEncoding.UTF_8);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".csv");
            out = response.getOutputStream();
            EasyExcelFactory.write(out).excelType(ExcelTypeEnum.CSV).sheet(sheetName).head(EasyExcelUtil.headList(headNameList)).doWrite(new ArrayList<>());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(out);
        }
    }
}
相关推荐
lay_liu2 小时前
springboot 文件下载
java·spring boot·后端
Flittly2 小时前
【SpringAIAlibaba新手村系列】(11)Embedding 向量化与向量数据库
java·笔记·spring·ai·springboot
ZPC82102 小时前
如何创建一个单例类 (Singleton)
开发语言·前端·人工智能
超绝振刀怪3 小时前
【C++多态】
开发语言·c++
workflower3 小时前
AI制造-推荐初始步骤
java·开发语言·人工智能·软件工程·制造·需求分析·软件需求
魔都吴所谓3 小时前
【Python】从零构建:IP地理位置查询实战指南
开发语言·python·tcp/ip
环黄金线HHJX.4 小时前
【吧里BaLi社区】
开发语言·人工智能·qt·编辑器
oioihoii4 小时前
Cursor根本无法调试C++
开发语言·c++
GISer_Jing4 小时前
Agent多代理架构:子代理核心机制解密
开发语言·人工智能·架构·aigc