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项目中有模板文件和文件夹

相关推荐
bugcome_com2 小时前
# C# 变量作用域详解
开发语言·c#
三方测试小学徒3 小时前
GB/T 34946-2017《C#语言源代码漏洞测试规范》之整体解读
c#·cma·cnas·34946
光泽雨4 小时前
P/Invok执行时的搜索顺序
c#
用户298698530144 小时前
C# Word自动化:轻松插入特殊符号,告别手动烦恼!
后端·c#·.net
光泽雨5 小时前
C#库文件调用逻辑
开发语言·c#
WarPigs5 小时前
Excel WPS表格笔记
excel·wps
kylezhao20196 小时前
C# 中的类型转换详解
c#
游乐码6 小时前
c#冒泡排序
c#·排序算法
fengyehongWorld7 小时前
Excel 切片器 快速过滤数据
excel