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

相关推荐
小金子会发光6 小时前
C# WinForms 手搓 Modbus RTU 调试助手:不依赖第三方 Modbus 库,从 CRC16 到 8 种功能码完整实现
c#·modbusrtu·串口通讯
上海安当技术9 小时前
C/S 桌面软件怎么接 UKey 强认证?C# SDK 与 C 动态库集成 WinForms/Qt/工控实战
c语言·qt·c#
明如正午11 小时前
【C#】跨线程更新进度条_UpdateProgress 方法解析
c#
一行注释11 小时前
纯前端通过xlsx.js导出Excel表格:完整示例与实战指南
前端·javascript·excel
曹牧12 小时前
C#:`JsonConvert.DeserializeObject`
windows·c#
QuartusII712 小时前
Excel中alt+enter单元格内无法换行
excel
望天hous12 小时前
C#中masstranist快速入门
开发语言·c#
Arya_aa13 小时前
EasyExcel读和写
excel
冷咖啡离 我的笨笨13 小时前
数据去重:通过 C# 删除 Excel 中的重复行
开发语言·c#·excel
鱼听禅16 小时前
C#学习笔记-添加编译参数到程序集自动更新程序编译时间
笔记·学习·c#