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

相关推荐
Scout-leaf9 小时前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m62511 小时前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech17 小时前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
Non-existent9871 天前
WPS批量清理单元格空白字符的4种方法-异常数字格式处理-实战
excel·wps
2601_962072551 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
Channing Lewis2 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
m0_547486662 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
叶帆2 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
jarreyer2 天前
【数据分析绘图】excel绘图和bi工具区别
数据挖掘·数据分析·excel
IT方大同2 天前
(嵌入式操作系统)信号量
嵌入式硬件·c#