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

相关推荐
淡海水40 分钟前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota1 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆1 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota1 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
SunnyDays10112 小时前
Java 设置 Excel 数据验证:整数、日期、文本长度、下拉列表和时间
java·excel·数据验证·下拉列表
fogota2 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头2 小时前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
Metaphor6922 小时前
使用 Python 设置 Excel 行列自适应 【代码示例】
python·excel
zxy284722530112 小时前
C# 音频倍速播放
c#·音频·naudio·soundtouch·倍速
Water_Sunzhipeng16 小时前
HandyControl Demo 设置特定框架记录
c#