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);
        }
    }
}
相关推荐
xuzhiqiang072418 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
时艰.18 小时前
订单系统历史数据归档方案
java
MediaTea19 小时前
Python:生成器表达式详解
开发语言·python
一只叫煤球的猫20 小时前
ThreadForge v1.1.0 发布:让 Java 并发更接近 Go 的开发体验
java·后端·性能优化
overmind20 小时前
oeasy Python 115 列表弹栈用pop删除指定索引
开发语言·python
014.21 小时前
2025最新jenkins保姆级教程!!!
java·运维·spring boot·spring·jenkins
浣熊88821 小时前
天机学堂虚拟机静态ip无法使用(重启后ip:192.168.150.101无法使用连接Mobaxterm数据库等等,或者无法使用修改之后的Hosts域名去访问nacos,jenkins)
java·微服务·虚拟机·天机学堂·重启之后静态ip用不了
心 -21 小时前
java八股文IOC
java
Never_Satisfied21 小时前
在c#中,使用windows自带功能将文件夹打包为ZIP
开发语言·windows·c#
hnxaoli21 小时前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股