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

相关推荐
Chen—LSN12 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
Data-Miner14 小时前
怎么用AI给Excel去重?先核对这7条再下手,别让好数据被删错:数以轻舟方案解析
人工智能·excel
淡海水15 小时前
00-Unity和C#的GC剖析与对比-全篇导览
unity·c#·游戏引擎·gc
fogota16 小时前
C# `string.Join` 处理 `List<int>`
windows·c#
Data-Miner16 小时前
做表格数据分析的AI工具怎么选?先对一下这三个真实需求,再看哪些真能落地
人工智能·数据分析·excel
Behavior18 小时前
Unity 手游动态更换 App 图标 — Android 与 iOS 双端技术方案
c#·unity3d·游戏开发
周杰伦fans19 小时前
构建真正智能的CAD AI绘图代理
开发语言·人工智能·c#
UIU1141 天前
递归算法与汉诺塔
c++·学习·算法·c#·递归
CSharp精选营1 天前
我用 ASP.NET Core 做了个水稻病虫害检查系统
c#·毕业设计·.net core·码农刚子
唐青枫1 天前
我用 C# 开发了一款电子发票自动整理工具
c#·.net