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

相关推荐
离陌在学C#3 小时前
C# 异步编程:从 async/await 到 Task 实战指南
开发语言·数据库·c#
一晌小贪欢5 小时前
python-第15天:Python 列表推导式
开发语言·python·excel·数据可视化·python办公
用户298698530147 小时前
PDF 转 HTML 全攻略:三种方法轻松搞定网页转换
c#·html·.net
geovindu7 小时前
CSharp: State Pattern
开发语言·后端·c#·.net·状态模式·行为模式
俊昭喜喜里8 小时前
C#中的decimal
开发语言·c#
tryxr10 小时前
Chat2Excel 项目通用服务开发
java·开发语言·excel·java项目开发
Jazz_z10 小时前
使用 Python 合并与取消合并 Excel 单元格
开发语言·python·excel
JustCheng1 天前
Dispacher(1/2)深入解析-详细使用
c#
CoderIsArt2 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
2601_962381862 天前
Python办公联动:Excel数据一键自动生成PPT图表
python·数据分析·自动化·excel·ppt