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);
        }
    }
}
相关推荐
程序猿小蒜32 分钟前
基于springboot的车辆管理系统设计与实现
java·数据库·spring boot·后端·spring·oracle
richxu2025100137 分钟前
C语言<<超全.超重要>>知识点总结
c语言·开发语言
2501_9160088939 分钟前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
胎粉仔39 分钟前
Swift 初阶 —— inout 参数 & 数据独占问题
开发语言·ios·swift·1024程序员节
Lizhihao_1 小时前
Python如何写Selenium全攻略
开发语言·python
zl9798991 小时前
SpringBoot-Web开发之Web原生组件注入
java·spring boot·spring
小羊学伽瓦1 小时前
【Java数据结构】——常见力扣题综合
java·数据结构·leetcode·1024程序员节
「QT(C++)开发工程师」2 小时前
嵌入式Lua脚本编程核心概念
开发语言·lua
I'm Jie2 小时前
(五)Gradle 依赖传递与冲突处理
java·spring boot·spring·kotlin·gradle·maven
我命由我123452 小时前
Spring Cloud - Spring Cloud 声明式接口调用(Fiegn 声明式接口调用概述、Fiegn 使用)
java·后端·spring·spring cloud·微服务·架构·java-ee