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

相关推荐
sinat_37511226几秒前
abap excel上传
excel·上传·sap·abap
无风听海10 分钟前
C# 中对象相等性判断的全面解析
开发语言·c#
寻星探路10 分钟前
【Python 全栈测开之路】Python 基础语法精讲(三):函数、容器类型与文件处理
java·开发语言·c++·人工智能·python·ai·c#
·云扬·28 分钟前
【实操教程】Excel文件转CSV并导入MySQL的完整步骤
android·mysql·excel
城数派28 分钟前
2019-2025年各区县逐月新房房价数据(Excel/Shp格式)
大数据·数据分析·excel
Elieal1 小时前
EasyExcel 实现 Excel 导入导出
java·excel
寻星探路1 小时前
【Python 全栈测开之路】Python 进阶:库的使用与第三方生态(标准库+Pip+实战)
java·开发语言·c++·python·ai·c#·pip
kylezhao20199 小时前
C#winform数据绑定
c#
爱吃西红柿鸡蛋面12 小时前
JsonHelper使用
c#
故事不长丨12 小时前
C#线程编程全解析:从基础应用到高级实践
c#·线程·多线程·thread·线程同步·异步编程·线程锁