下载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();
            }
        }
    }
相关推荐
潼心1412o1 天前
C语言(长期更新)第15讲 指针详解(五):习题实战
c语言·开发语言
Learn Beyond Limits1 天前
Transfer Learning|迁移学习
人工智能·python·深度学习·神经网络·机器学习·ai·吴恩达
Murphy_lx1 天前
Lambda表达式
开发语言·c++
yangpipi-1 天前
C++并发编程-23. 线程间切分任务的方法
开发语言·c++
love530love1 天前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
爬虫程序猿1 天前
利用 Java 爬虫获取淘宝商品 SKU 详细信息实战指南
java·开发语言·爬虫
F2E_Zhangmo1 天前
基于cornerstone3D的dicom影像浏览器 第五章 在Displayer四个角落显示信息
开发语言·前端·javascript
He1955011 天前
Go初级之十:错误处理与程序健壮性
开发语言·python·golang