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

相关推荐
zxy28472253014 分钟前
使用Topshelf部署window后台服务(C#)
c#·安装·topshelf·后台服务
缺点内向2 小时前
C# 高效统计 Word 文档字数:告别手动,拥抱自动化
c#·自动化·word
weixin_462446233 小时前
Python 实战:将 HTML 表格一键导出为 Excel(xlsx)
linux·python·excel·pandas
skywalk81633 小时前
介绍一下 Backtrader量化框架(C# 回测快)
开发语言·c#·量化
Never_Satisfied3 小时前
C#数组去重方法总结
开发语言·c#
阿蒙Amon3 小时前
C#每日面试题-静态构造函数和普通构造函数区别
java·开发语言·c#
Java程序员威哥3 小时前
SpringBoot4.0+JDK25+GraalVM:云原生Java的性能革命与落地指南
java·开发语言·后端·python·云原生·c#
Data-Miner3 小时前
Excel-Agent永久买断,一款结合AI Agent的excel数据处理智能体
人工智能·excel
阿蒙Amon3 小时前
C#每日面试题-Task和ValueTask区别
java·开发语言·c#
慧都小项4 小时前
文档开发组件Aspose 26.1更新:兼容.net 10、修复显示数据问题
c#