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

相关推荐
MyBFuture3 小时前
C#接口与抽象类:关键区别详解
开发语言·c#·visual studio
曹牧4 小时前
C#:记录日志
服务器·前端·c#
心疼你的一切7 小时前
三菱FX5U PLC与C#通信开发指南
开发语言·单片机·c#
czhc11400756637 小时前
C# 1221
java·servlet·c#
娃乐呵8 小时前
免费的大批量Excel文档大模型处理数据工具
语言模型·大模型·excel·数据处理
yong99909 小时前
C#实现OPC客户端与S7-1200 PLC的通信
开发语言·网络·算法·c#
先生沉默先10 小时前
c#Socket学习,使用Socket创建一个在线聊天,服务端功能实现,(3)
服务器·学习·c#
superman超哥10 小时前
仓颉热点代码识别深度解析
开发语言·后端·python·c#·仓颉
Lv117700810 小时前
Visual Studio中的接口
ide·笔记·c#·visual studio
Eiceblue11 小时前
使用 Python 写入多类型数据至 Excel 文件
开发语言·python·excel