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);
        }
    }
}
相关推荐
马猴烧酒.10 分钟前
【JAVA数据传输】Java 数据传输与转换详解笔记
java·数据库·笔记·tomcat·mybatis
爱编码的傅同学12 分钟前
【常见锁的概念】死锁的产生与避免
java·开发语言
Tansmjs25 分钟前
实时数据可视化库
开发语言·c++·算法
我什么都学不会32 分钟前
Python练习作业3
开发语言·python
rabbit_pro39 分钟前
SpringBoot3使用PostGis+GeoTools整合MybatisPlus
java·spring
2401_838472511 小时前
C++模拟器开发实践
开发语言·c++·算法
初九之潜龙勿用1 小时前
C# 操作Word模拟解析HTML标记之背景色
开发语言·c#·word·.net·office
froginwe111 小时前
MySQL UNION 操作详解
开发语言
ruxshui1 小时前
Python多线程环境下连接对象的线程安全管理规范
开发语言·数据库·python·sql
雨季6661 小时前
Flutter 三端应用实战:OpenHarmony 简易点击计数器与循环颜色反馈器开发指南
开发语言·flutter·ui·ecmascript·dart