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

相关推荐
吴可可1235 小时前
C#实现CAD框选闭合图元外偏移1毫米
c#
꒰ঌ 安卓开发໒꒱5 小时前
.NET CAP入门到入土
后端·c#·.net
逝水无殇9 小时前
C# 枚举(Enum)详解
开发语言·后端·c#
未来之窗软件服务10 小时前
Excel物业台帐租金合同资产—万象EXCEL应用(28) —东方仙盟
excel·仙盟创梦ide·东方仙盟·万象excel
Java面试题总结11 小时前
C# 源生成器使用方法
windows·ui·c#
影寂ldy11 小时前
C# WinForms 窗体继承
开发语言·c#
xurime1 天前
Excelize 开源十周年,发布 2.11.0 版本
golang·开源·github·excel·导出·导入·excelize·基础库
苍狼唤1 天前
WinForm练习知识补充(多线程)
c#
逝水无殇1 天前
C# 字符串(String)详解
开发语言·后端·c#
小巧的砖头1 天前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#