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(); 
} 
相关推荐
城数派15 小时前
2026年500米分辨率DEM地形数据(全球/全国/分省/分市)
数据库·arcgis·信息可视化·数据分析·excel
SunnyDays101116 小时前
Python 操作 Excel 超链接:添加网页、文件、工作表和图片链接
python·excel
专注VB编程开发20年19 小时前
我制作excel工作簿的选项卡,发给deep seek, 昨天修改了一天
前端·vue.js·excel
星越华夏2 天前
python办公自动化,csv文件/excel文件差集合并
开发语言·python·excel
开开心心就好2 天前
180套模板的图片艺术拼接实用工具
linux·服务器·网络·spring·智能手机·maven·excel
仰望.2 天前
vxe-table 导出 Excel 进阶教程:自定义样式与高级功能
前端·javascript·vue.js·excel·vxe-table·vxe-ui
Access开发易登软件2 天前
Access 和 SQLite,根本不在一个赛道上
java·jvm·数据库·sqlite·excel·vba·access开发
biuyyyxxx3 天前
Excel常见异常
excel
猫猫不是喵喵.3 天前
关于解析Excel中的日期出现是数字序列的问题
excel
asdzx673 天前
Python TXT 转 Excel (自动识别分隔符)
python·excel