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

相关推荐
曹牧13 小时前
C#:输入的字符串格式不正确
c#
Allstar_4316 小时前
制造业研发管理数字化:从 Excel 到专业 APQP 系统的架构选型思考
架构·excel
驾驭人生16 小时前
NET NPOI 实战:生成带下拉、数据校验、必填标红的 Excel 导入模板
excel
泡海椒16 小时前
把 TRANSFORM 做成可扩展能力:JQuick-Excel 的函数与 SPI 调用链
驱动开发·excel
根号五18 小时前
JetBrains Rider 使用教程(1)新建解决方案
c#·jetbrains rider
2601_9627809118 小时前
2027采购岗准备:大数据管理与应用专业如何把Excel、SQL变成业务能力
excel
海天鹰19 小时前
C#:接收从打开方式打开的参数
c#
宝桥南山19 小时前
.NET 11 - 尝试一下Minimal API中的异步验证新特性
microsoft·微软·c#·asp.net·.net·.netcore
开开心心_Every20 小时前
文件查重软件批量删除重复文件释放空间
java·开发语言·c#·计算机外设·ocr·excel·perl
波力海苔夹心脆67520 小时前
VisionPro 与 VisionMaster 对比(上):软件定位、开发环境与核心视觉工具对照
图像处理·算法·计算机视觉·c#·视觉检测·个人开发·vp9