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(); 
} 
相关推荐
香煎三文鱼9 小时前
C# net8生成excel,并设置列规则导出文件
c#·excel·c#生成ecel
流形填表12 小时前
word表格批量转excel,提取表格数据到excel
word·excel
Dfreedom.1 天前
Excel文件数据的读取和处理方法——C++
c++·数据分析·excel·数据预处理
开开心心就好1 天前
Word批量转PDF工具
开发语言·人工智能·pdf·c#·vim·excel·语音识别
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建组合图表
c#·excel·aspose·图表
小镇学者2 天前
【PHP】导入excel 报错Trying to access array offset on value of type int
android·php·excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中应用数据验证
c#·excel·aspose·文档开发·文档处理
雾林小妖2 天前
POI设置Excel单元格背景色
excel·poi设置背景色
只有干货2 天前
java若依 excel 导出高度自适应
java·excel