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

相关推荐
zimoyin44 分钟前
Windows 控件获取全攻略:Win32 / UIA / MSAA + 鼠标拾取与点击关联(C# 实战)
c#·自动化
ms365copilot2 小时前
Excel Copilot 一键定位修改的内容
excel·copilot
小羊没烦恼!11 小时前
铁道部新客票系统设计(二)
开发语言·前端·数据库·python·oracle·c#·php
昂好的day12 小时前
VisionMaster 通信模块实战(上):Modbus、TCP 收发、协议组装解析
开发语言·网络·网络协议·tcp/ip·c#
波力海苔夹心脆67515 小时前
C# 正则表达式零基础入门:从“看得懂“到“会用“
经验分享·后端·mysql·正则表达式·c#
昂好的day17 小时前
VisionMaster(VM) 标定工具详解(上):为什么必须标定 + 九点标定手眼标定
开发语言·数码相机·c#
小羊没烦恼!1 天前
深入浅出SQL Server中的死锁
java·开发语言·前端·数据库·后端·spring·c#
帆张芳显1 天前
前端表格公式能力新选择|智表 ZCell 公式引擎深度剖析
前端·excel·canva可画·excel插件
地图小课堂1 天前
Bigemap Pro 导入 Excel、CSV、TXT 点位表格教程
excel
我的愿望是成为富婆1 天前
2027校招准备:从Excel操作到数据分析,还缺业务、SQL与统计思维
sql·数据分析·excel