SpringBoot下载Excel模板功能

embedded/2024/10/18 3:21:39/

目录

一、前端只需要填写一个a标签调用一下后端接口即可

二、后端

2.1 准备一个excel模板 ,将其复制到resource目录下的templates文件夹下

2.2 接着复制下列代码即可

三、运行效果


一、前端只需要填写一个a标签调用一下后端接口即可

1.1 先代理一下防止跨域

export default defineConfig({plugins: [vue()],server: {port: 5000,proxy: {"/api": { target: "http://localhost:8080/", changeOrigin: true },},},});

2.2 调用后端接口

二、后端

2.1 准备一个excel模板 ,将其复制到resource目录下的templates文件夹下

2.2 接着复制下列代码即可

java">package com.beiyou.controller;import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;@RestController
@RequestMapping("/api")
public class DownloadController {@GetMapping("/downloadExcelTemplate")public ResponseEntity<InputStreamResource> downloadExcelTemplate() throws Exception {// 模板文件路径String templateFilePath = "/templates/123.xls";// 获取资源文件输入流ClassPathResource resource = new ClassPathResource(templateFilePath);InputStream inputStream = resource.getInputStream();// 设置响应头,包括文件名和MIME类型String filename = "模板.xlsx";String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);headers.setContentDispositionFormData("attachment", encodedFilename);// 返回包含文件流的ResponseEntity对象return ResponseEntity.ok().headers(headers).body(new InputStreamResource(inputStream));}
}

三、运行效果


http://www.ppmy.cn/embedded/19984.html

相关文章

Linux - tar (tape archive)

tar 的全称是 Tape Archive。它最初是在 Unix 系统中用于将数据写入磁带的工具&#xff0c;但现在它通常用于创建、维护、修改和提取文件的归档文件。尽管 tar 可以用于压缩和解压缩文件&#xff0c;但它本身并不进行压缩&#xff0c;而是通常与 gzip 或 bzip2 等压缩工具一起使…

Navicat连接SQL server出现:[IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序(0)

问题 解决方法 一 找到Navicat的安装路径&#xff0c;然后找到sqlncli_x64.msi文件并安装&#xff0c;安装成功后重启Navicat重新进行连接&#xff0c;看是否成功。 解决方法 二 如果方法一没有找到找到sqlncli_x64.msi 还是Navicat的安装路径&#xff0c;然后找到msodbcsql_64…

算法人生(11):从“梯度提升树(GBDT)”看“2/8时间管理法”

梯度提升树&#xff08;Gradient Boosting Decision Trees, GBDT&#xff09;是一种高效的机器学习算法&#xff0c;它通过迭代构建多个决策树并综合它们的预测结果来提高预测的准确率。GBDT的核心思想在于&#xff0c;每一棵树都尝试对前一棵树留下的预测残差进行修正&#xf…

[c++]菱形继承解析

菱形继承 大概示意图&#xff1a; 菱形继承不一定只是标准的菱形&#xff0c;只要形似菱形的都可以叫菱形继承。 (以下说明都是默认公有继承&#xff0c;public和protected成员情况下) 菱形继承会造成数据的冗余和二义性&#xff1a; 冗余&#xff1a;一个Assitant对象里面有…

tensorflow_decision_forests\tensorflow\ops\inference\inference.so not found

恰好有一个帖子提到了py3.10里面的解决方案 pip install --user tensorflow2.11.0My tensorflow version is 2.11.0 and my tensorflow_decision_forests version is 1.2.0 so those should be compatible. I also am using Python version 3.10.11原文链接&#xff1a; http…

leetCode59. 螺旋矩阵 II

leetCode59. 螺旋矩阵 II 题目思路&#xff1a;见我的这篇博客 代码 class Solution { public:vector<vector<int>> generateMatrix(int n) {// n * n的矩阵的初始化vector<vector<int>> res(n, vector<int> (n));// 方向数组int dx[] {0, 1…

第10章:知识整合提示

这种技术使用模型的预先存在的知识&#xff0c;来整合新、旧信息&#xff0c;助力我们全面认知特定的主题。 与ChatGPT配合时&#xff0c;只需提供新信息与任务目标&#xff0c;加上清晰的提示词&#xff0c;它就能为你生成满意文本。 例 1:知识整合 任务&#xff1a;将新信息…

无人机GB42590接收端 +接收端模组,同时支持2.4G与5.8G双频

严格按照GB42590的协议开发的发射端&#xff0c;通过串口和模块通讯&#xff0c;默认波特率 921600。 http://www.doit.am/深圳四博智联科技有限公司https://shenzhendoit.taobao.com/category-1734422372.htm?spma1z10.1-c-s.0.0.560c74d77eT01G&searchy&catNameGB4…