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

相关推荐
鸽子一号1 小时前
c#Modbus通信
开发语言·c#
cjp5604 小时前
001.Blazor简介
c#
工程师0075 小时前
C# 程序集、IL、CLR 执行流程
c#·clr·il·程序集
xxjj998a5 小时前
PHP vs C#:核心差异全解析
开发语言·c#·php
我不在你不在6 小时前
C# 异步与LINQ实战亮点
c#
游乐码6 小时前
c#预处理器指令
c#
专注VB编程开发20年6 小时前
Excel 2024 / 365 已经把 “单元格填充效果→图片” 这个按钮删掉了
excel
之歆7 小时前
DAY13_CSS3进阶完全指南 —— 背景、边框、文本、渐变、滤镜与 Web 字体(上)
前端·c#·css3
工程师00716 小时前
C# 装箱、拆箱 底层原理
c#·装箱和拆箱
清风明月一壶酒16 小时前
OpenClaw自动处理Word文档全流程
开发语言·c#·word