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

相关推荐
njsgcs44 分钟前
c# solidworks 创建装配体工程图+bom
开发语言·c#·solidworks
njsgcs1 小时前
c# solidworks 工程图获得展开视图不在固定面螺纹特征的位置
开发语言·c#·solidworks
深海潜水员4 小时前
【从零开始的C#游戏开发课程】- FarmStory1.0 日志系统和游戏资源的管理
游戏·c#·monogame
叶帆4 小时前
【YFIOs】用C#开发硬件之WiFi网络
开发语言·网络·c#
chatexcel4 小时前
ChatExcel MAX 教程:AI Excel 数据清洗、异常核查与分析报告生成
人工智能·excel
天下无敌笨笨熊5 小时前
C# LINQ开发心得
c#·linq
葡萄城技术团队5 小时前
模拟运算表全解析:从 Excel 的两变量限制到 SpreadJS 的不限变量 | SpreadJS 新版本 特性解析
excel
小白不白1116 小时前
Invoke的用法
开发语言·人工智能·数码相机·计算机视觉·c#
FuckPatience7 小时前
C# 链表元素的引用地址分析
链表·c#
hoiii1877 小时前
C# Txt/Excel/Access 导入导出工具
开发语言·c#·excel