vue 前端下载excel的n中方法

阅读: 评论:0

vue 前端下载excel的n中方法

vue 前端下载excel的n中方法

1、直接下载html中的表格元素

	<table id="table" border="1"><tr style="height:50px;"><th style="width:100px;color:red;">姓名</th><th>性别</th><th>年龄</th></tr></table><a id="down">点击下载excel</a><script>let html = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"></head><body>${ElementById('table').outerHTML}</body></html>`let blob = new Blob([html],{ type: 'application/vnd.ms-excel'});let a = ElementById('down');a.href = ateObjectURL(blob);a.download = '测试excel下载'</script>原文链接:

2、npm install node-xlsx

接口返回的数据,再处理成map对象或者数组对象,再写入到excel


downloadFile(){const xlsx = require('xlsx');let arrayData = [['name', 'age'],['zhangsan', 20]];let jsonData = [{name: "zhangsan1",age: 30},{name: "zhangsan1",age: 24}];// 将数据转成workSheetlet arrayWorkSheet = xlsx.utils.aoa_to_sheet(arrayData);let jsonWorkSheet = xlsx.utils.json_to_sheet(jsonData);// 构造workBooklet workBook = {SheetNames: ['arrayWorkSheet', 'jsonWorkSheet'],Sheets: {'arrayWorkSheet': arrayWorkSheet,'jsonWorkSheet': jsonWorkSheet,}};// 将workBook写入文件xlsx.writeFile(workBook, "./aa.xlsx");
}

本文发布于:2024-01-29 15:35:33,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170651373716288.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:方法   vue   excel
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23