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);
        }
    }
}
相关推荐
鲸沉梦落1 小时前
Java中的Stream
java
yihuiComeOn1 小时前
[源码系列:手写Spring] AOP第二节:JDK动态代理 - 当AOP遇见动态代理的浪漫邂逅
java·后端·spring
p***h6432 小时前
JavaScript在Node.js中的异步编程
开发语言·javascript·node.js
散峰而望2 小时前
C++数组(二)(算法竞赛)
开发语言·c++·算法·github
Porunarufu2 小时前
Java·关于List
java·开发语言
靠沿2 小时前
Java数据结构初阶——Collection、List的介绍与ArrayList
java·数据结构·list
程序猿小蒜2 小时前
基于springboot的的学生干部管理系统开发与设计
java·前端·spring boot·后端·spring
子不语1803 小时前
Python——函数
开发语言·python
q***56383 小时前
Spring容器初始化扩展点:ApplicationContextInitializer
java·后端·spring
ndjnddjxn3 小时前
Rust学习
开发语言·学习·rust