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

相关推荐
曹牧3 小时前
C#:窗体Topmost与获取输入焦点
c#
脉动数据行情15 小时前
C# 实现德指 DAX 期货 WebSocket 实时行情监听
开发语言·websocket·c#
码士集团小青5 小时前
C# 手写向量化能追上 C 吗?
c#
The Future is mine6 小时前
Excel如何只允许对部分表格内容进行修改
excel
c#上位机7 小时前
tuple_max求数组中的最大值
c#·halcon·机器视觉
2601_962099467 小时前
Python xlwt设置excel单元格字体及格式
python·excel·xlwt·样式设置·单元格格式
xintaiideas11 小时前
.NET 项目的多方式启动指南 [特殊字符]
c#
Z59981784112 小时前
c#软件开发学习笔记--WPF(MVVM框架)
笔记·学习·c#
软件黑马王子14 小时前
19.资源加载模块:主要作用和基本原理
开发语言·前端框架·c#
Am-Chestnuts16 小时前
通义千问任务清单怎么导出Word?用DS随心转保留层级和勾选项
开发语言·c#·word