Vue3下载WEBAPI导出的Excel文件

webApi查询数据保存为Excel

复制代码
       /// <summary>
       /// 获取LMI3D相机涂胶测量数据
       /// </summary>
       /// <returns></returns>
       [HttpPost(Name = "GetLMI3DGlueDataToExcel")]
       public async Task<IActionResult> GetLMI3DGlueDataToExcel(QueryGlueMeasurementModel qgmm)
       {
           List<GlueMeasurementModel> result = new List<GlueMeasurementModel>();
           result = await LinkAsiaDB.Camera3D.GetGlueMeasurement( qgmm.CreatedDate);

           int x = await NPOIExcelHelper.RenderToExcel(result, "defult", "D:\\Test.xlsx");

           string fileExt = Path.GetExtension("D:\\Test.xlsx");
           //获取文件的ContentType
           var provider = new FileExtensionContentTypeProvider();
           var memi = provider.Mappings[fileExt];
           var fileBytes = System.IO.File.ReadAllBytes("D:\\Test.xlsx");
           return File(fileBytes, memi, "welcome.xlsx");
       }

swagger中可以下载

Vue页面

复制代码
			<el-row>
				<el-date-picker v-model="QueryDate" type="date" placeholder="Pick a day" :size="size" />

				<el-button type="primary" @click="QueryFoamMeasurement()">
					查询
				</el-button>
				<el-button type="primary" @click="ToExcel()">
					导出excel
				</el-button>
				<a :href="DownloadUrl" download="welcom.xlsx">abc</a>
			</el-row>

导出方法

复制代码
const ToExcel = async () => {

	let CreatedDate = dateFormat(QueryDate.value, 'YYYY-MM-DD')
  
	let pa = { No: "", Barcode: "", CreatedDate: CreatedDate }
	await axios.post(global_const.WEBAPI + `LMI3DData/GetLMI3DGlueDataToExcel`, pa, { responseType: 'blob' })
		.then(function (response) { 
			console.log(response); 
			DownloadUrl.value = window.URL.createObjectURL(new Blob([response.data]));
	 
			return response.data;
		})
		.catch(function (error) {
			//ElMessage.error(cmd + '命令执行异常!' + error)
			console.log(error);
		});

		download(DownloadUrl.value )
}

const download = (url :any) => {
  // 处理图片下载
 
  let a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("download",'');
    a.setAttribute("target", "_blank");
	a.download = "welcome1.xlsx"
	a.click(); 
} 
相关推荐
Mr.456717 小时前
Spring Boot 3 + EasyExcel 3.x 实战:构建高效、可靠的Excel导入导出服务
spring boot·后端·excel
如意机反光镜裸17 小时前
excel怎么快速导入oracle
数据库·oracle·excel
开开心心就好18 小时前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse
城数派1 天前
2015-2025年我国区县逐年二手房房价数据(Excel/Shp格式)
excel
用户8356290780511 天前
Python 设置 Excel 条件格式教程
后端·python·excel
lzksword2 天前
关于EXCEL中vlookup身份证匹配失败的处理
excel
城数派2 天前
1990-2025年我国省市县三级的逐年土地覆盖数据(9类用地/Excel/Shp格式)
excel
catoop2 天前
构建高稳健性、可交互的复杂 Excel 报表方法论:切片、流式与动态公式
excel
葡萄城技术团队2 天前
Excel VBA 核心概念全解析:宏、模块、过程的区别与联系(含 SpreadJS Web 替代方案)
excel