Vue3下载WEBAPI导出的Excel文件

webApi查询数据保存为Excel 

       /// 
       /// 获取LMI3D相机涂胶测量数据
       /// 
       /// 
       [HttpPost(Name = "GetLMI3DGlueDataToExcel")]
       public async Task GetLMI3DGlueDataToExcel(QueryGlueMeasurementModel qgmm)
       {
           List result = new List();
           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中可以下载 

Vue3下载WEBAPI导出的Excel文件_第1张图片 

Vue页面 

			
				

				
					查询
				
				
					导出excel
				
				abc
			

导出方法 

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(); 
} 

你可能感兴趣的:(VUE3,WEBAPI,excel)