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

相关推荐
用户2986985301416 分钟前
前端实战:在 React 中使用 JavaScript 一键导出 Excel 图表与形状为图片
javascript·react.js·excel
Q一件事2 小时前
arcpy实现分区统计到一张excel中
excel
AI刀刀2 小时前
deepseek 内容粘贴后符号丢失怎么办?AI 导出鸭实测解决排版乱码问题
android·人工智能·excel·ai导出鸭
-银雾鸢尾-2 小时前
C#中的万物之父——Object类
开发语言·c#
-银雾鸢尾-2 小时前
C#中的继承
开发语言·c#
吴可可12313 小时前
C#AutoCAD二次开发打断多段线
c#
-银雾鸢尾-15 小时前
里氏替换原则
开发语言·c#·里氏替换原则
-银雾鸢尾-19 小时前
C#中的索引器
开发语言·c#
en.en..19 小时前
冒泡排序与选择排序完整对比解析
开发语言·数据结构·算法·c#·排序算法
geovindu20 小时前
CSharp: Decorator Pattern
开发语言·后端·c#·装饰器模式·结构型模式