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

相关推荐
周杰伦fans2 小时前
C# 踩坑 CS8370:Switch Expression 在 C# 7.3 不可用及三种解决方案
c#
z落落4 小时前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
xiaoshuaishuai85 小时前
C# AvaloniaUI动态显示图片
开发语言·c#
csdn_aspnet8 小时前
EasyModbus 与 C# 集成
c#·modbus·easymodbus
SunnyDays10119 小时前
Python操作Excel批注:从基础添加到高级自定义的完整指南
开发语言·python·excel
JaydenAI10 小时前
[MAF预定义ChatClient中间件-06]利用ImageGeneratingChatClient开发专业图片生成Agent
ai·c#·agent·agent管道·chatclient中间件·chatclient管道
Eiceblue11 小时前
Python 操作 Excel:数据分组、分类汇总与取消分组全解
开发语言·python·excel
csdn_aspnet11 小时前
Modbus TCP C# 客户端程序
服务器·网络·tcp/ip·c#
城数派12 小时前
2026年500米分辨率DEM地形数据(全球/全国/分省/分市)
数据库·arcgis·信息可视化·数据分析·excel