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

相关推荐
AI_567811 小时前
Excel数据透视表提速:Power Query预处理百万数据
数据库·excel
游乐码13 小时前
c#变长关键字和参数默认值
学习·c#
全栈小513 小时前
【C#】合理使用DeepSeek相关AI应用为我们提供强有力的开发工具,在.net core 6.0框架下使用JsonNode动态解析json字符串,如何正确使用单问号和双问号做好空值处理
人工智能·c#·json·.netcore·deepseek
颜挺锐13 小时前
EXCEL中A列连接B列中间加| 怎么写公式
excel
wearegogog12313 小时前
基于C#的TCP/IP通信客户端与服务器
服务器·tcp/ip·c#
A_nanda19 小时前
C#调用Quartz.NET的完整实现。
c#·.net·quartz
2501_9307077820 小时前
使用C#代码在 PowerPoint 演示文稿中插入表格
开发语言·c#·powerpoint
少控科技20 小时前
C#基础训练营 - 01 - 数据类型
开发语言·c#
1314lay_100721 小时前
Vue3 + Element Plus项目和C# .Net 7.0 Core后端API项目发布部署到服务器
服务器·前端·javascript·vue.js·elementui·c#·.net
郝亚军21 小时前
c#如何编译、通过icd文件生成static_model.c和static_model.h
开发语言·c#