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);
        }
    }
}
相关推荐
米羊12116 分钟前
风险评估文档记录
开发语言·网络·php
摘星编程20 分钟前
解锁Agent智能体的未来:五大实战策略彻底革新人机协作模式
java·开发语言
百块富翁21 分钟前
可管控、不重复TraceId解决方案
java·分布式·系统架构
金銀銅鐵26 分钟前
浅解 Junit 4 第二篇: Runner 和 ParentRunner
java·junit·单元测试
Aerkui39 分钟前
Go 泛型(Generics)详解
开发语言·后端·golang
_codemonster40 分钟前
JavaWeb开发系列(七)表单开发
java
clive.li41 分钟前
go-webmvc框架推荐
开发语言·后端·golang
寻寻觅觅☆1 小时前
东华OJ-基础题-127-我素故我在(C++)
开发语言·c++·算法
ab1515171 小时前
2.13完成101、102、89
开发语言·c++·算法
切糕师学AI1 小时前
NFS(网络文件系统)详解
开发语言·网络·php