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

相关推荐
github.com/starRTC3 小时前
Claude Code中英文系列教程34:再谈Skills
开发语言·c#·ai编程
bugcome_com5 小时前
C# 判断语句详解(if、switch、三目运算符、Null 条件运算符)
c#
霸王•吕布8 小时前
C#-使用OpenTK渲染3D模型
c#·opentk·glcontrol
游乐码8 小时前
c#封装成员变量和成员方法和访问修饰符
开发语言·c#
Never_Satisfied8 小时前
在c#中,Jint的AsString()和ToString()的区别
服务器·开发语言·c#
Never_Satisfied8 小时前
在c#中,获取文件的大小
java·开发语言·c#
weixin_468466859 小时前
PyTorch导出ONNX格式分割模型及在C#中调用预测
人工智能·pytorch·深度学习·c#·跨平台·onnx·语义分割
光泽雨17 小时前
C# 中 Assembly 类详解
开发语言·c#
少控科技17 小时前
C#基础训练营 - 02 - 运算器
开发语言·c#
两千次1 天前
图像的处理 图片裁剪工具方法 图片按比例缩放的工具方法
c#