下载excel模板

前言:

复制代码
			Excel模板在你当前项目相对路径下,如下图所示
			![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/643ffdb76d6f4769a5793ccf65d41739.png)

代码如下:(为了直观,统一放在控制层)

java 复制代码
/**
     * 下载模板
     * @param response
     * @return
     */
    @SneakyThrows
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){
        String fileName = "指标体系导入模板.xlsx";
        ServletOutputStream out = response.getOutputStream();

        try{
            //文件在项目中的存放路径
            String filePath = getClass().getResource("/static/" + fileName).getPath();    // 文件路径就是上图添的位置
            filePath = URLDecoder.decode(filePath, "UTF-8");

            //设置响应
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            FileInputStream inputStream = new FileInputStream(filePath);

            int b = 0;
            byte[] buffer = new byte[1024];
            while ((b = inputStream.read(buffer)) != -1) {
                // 写到输出流(out)中
                out.write(buffer, 0, b);
            }
            inputStream.close();
        }catch (Throwable e){
            e.printStackTrace();
        }finally{
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
相关推荐
xlq223225 小时前
22.多态(上)
开发语言·c++·算法
666HZ6666 小时前
C语言——高精度加法
c语言·开发语言·算法
Wise玩转AI6 小时前
Day 27|智能体的 UI 与用户交互层
人工智能·python·ui·ai·chatgpt·ai智能体
星释6 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
s***46986 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
风生u6 小时前
go进阶语法
开发语言·后端·golang
666HZ6666 小时前
C语言——黑店
c语言·开发语言
Gomiko7 小时前
JavaScript基础(八):函数
开发语言·javascript·ecmascript
runepic7 小时前
Python + PostgreSQL 批量图片分发脚本:分类、去重、断点续拷贝
服务器·数据库·python·postgresql
〝七夜5697 小时前
JVM内存结构
java·开发语言·jvm