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(); 
} 
相关推荐
TracyDemo10 小时前
excel 透视图怎么进行删除透视图
excel
骆驼爱记录10 小时前
Excel邮件合并嵌入图片技巧
自动化·word·excel·wps·新人首发
avi911112 小时前
Unity Data Excel读取方法+踩坑记;和WPS Excel的一些命令
unity·游戏引擎·excel·wps·data
梦幻通灵13 小时前
Excel多个sheet合并透视表实现方案【持续更新】
excel
开开心心就好13 小时前
键盘映射工具改键位,绿色版设置后重启生效
网络·windows·tcp/ip·pdf·计算机外设·电脑·excel
恬淡如雪14 小时前
Excel接口测试自动化实战
爬虫·python·excel
速易达网络14 小时前
linux命令大全
linux·运维·excel
Leisure -_-14 小时前
新建时没有excel选项,如何添加?
excel
骆驼爱记录2 天前
Word题注编号间距调整4种方法
自动化·word·excel·wps·新人首发
Eiceblue2 天前
两种实用方法删除 Excel 重复行:Python + VBA
开发语言·python·excel