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

相关推荐
开开心心就好15 分钟前
一键扫描电脑重复文件的实用工具
linux·运维·服务器·随机森林·智能手机·excel·启发式算法
ModestCoder_22 分钟前
Git 忽略所有 `.xlsx`,但保留指定 Excel 文件的方法
git·excel
神仙别闹4 小时前
基于C# 利用工程活动图 AOE 网设计算法
算法·c#·php
游乐码4 小时前
c#迭代器
开发语言·c#
海盗12344 小时前
C# OPC UA客户端开发实战
服务器·开发语言·c#
asdzx674 小时前
使用 C# 从 URL 下载 Word 文档
开发语言·c#·word
xiaoshuaishuai85 小时前
C# DeepSeek V4 与 V3对比
开发语言·c#·量子计算
游乐码6 小时前
c#特殊语法
开发语言·c#
z19408920667 小时前
在线题库整理:把重复劳动从 Excel 手工里解放出来
excel
.NET修仙日记7 小时前
2026 .NET 面试八股文:高频题 + 答案 + 原理(高级核心篇)
面试·职场和发展·c#·.net·.net core·.net 8