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#:字符串做20位截断
开发语言·c#
离陌在学C#11 小时前
C# 事件(Event)详解:从概念到实战
开发语言·c#
rockey62713 小时前
C#脚本引擎之AScript与Flee对比
c#·.net·script·eval·动态脚本
zjnlswd19 小时前
C#学习笔记4
笔记·学习·c#
曹牧20 小时前
C#:List<string>特定格式输出
windows·c#
Z59981784121 小时前
c#软件开发学习笔记--Modbus-TCP/UDP网口通讯
笔记·学习·c#
曹牧1 天前
C#:取的多个药品批准文号中的一个
c#
AI导出鸭1 天前
怎么让Claude做表格?AI导出鸭苹果版将Claude输出的Markdown表格或结构化列表智能解析为二维数据,一键导出Excel/Word标准表格。
人工智能·chatgpt·word·excel·ai导出鸭
用户298698530141 天前
3 种方法,轻松将 PowerPoint 转换为 PDF 格式
人工智能·后端·c#
KindSuper_liu1 天前
HybridCLR DHE及MetaVersion工作流核心概念
c#