C# 下载模板文件 Excel

后端代码

cs 复制代码
[HttpGet("DownloadExcel")]
  public async Task<dynamic> DownloadExcel(string tmplName)
        {
            var _fileName = "导入表模板.xlsx";
            
            var filePath = @"Files\DownLoad\";
            var NewFile = Path.Combine(filePath, tmplName);
            var stream = new FileStream(NewFile, FileMode.Open);
            return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }

前端代码 vue 2.0

javascript 复制代码
// 下载导入表模板
    downloadExcel() {
      Axios.get('/api/Excel/DownloadExcel', {
        params: {
          tmplName: 'Excel'
        },
        responseType: 'blob',
      })
          .then(res => {
            const link = document.createElement('a')
            // const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
            const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            link.download = 'xxx入表模板.xlsx'
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          })
          .catch(error => {
            console.log(error)
          })
    },

注意: 发布的WebAPI项目中有模板文件和文件夹

相关推荐
骆驼爱记录16 分钟前
Python打包命令全攻略
自动化·word·excel·新人首发
TDengine (老段)27 分钟前
TDengine C# 语言连接器入门指南
大数据·数据库·c#·时序数据库·tdengine·涛思数据
凯新生物33 分钟前
Mannose-PEG-CY5.5,CY5.5-PEG-Mannose技术手册:分子量选型与溶解性说明
javascript·c#·bash·symfony
wtsolutions34 分钟前
JSON to Excel WPS Add-in - Perfect for WPS Office Users
json·excel·wps
hixiong1231 小时前
C# OpenVinoSharp部署Yolo26模型进行推理
开发语言·c#·openvino·yolo26
hhzz2 小时前
Springboot项目中使用EasyPOI操作Excel(详细教程系列4/4)
java·spring boot·后端·spring·excel·poi·easypoi
观无2 小时前
VisionPro的二维码识别
c#
Fairy要carry2 小时前
面试:LLM-分词
开发语言·c#
kylezhao201913 小时前
C# 文件的输入与输出(I/O)详解
java·算法·c#
kylezhao201914 小时前
C# TreeView 控件详解与应用
c#