前端js导出excel表格
1、安装 依赖模块 npm install xlsx -D
2、 创建html文件
<script lang="javascript" src="./node_modules/xlsx/xlsx.mini.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
3、使用
<script type="text/javascript">
window.onload = function () {
$.ajax({
url: "https://xxx/biz.h5/home/progRing?channelNo=84322&progNo=240749&pageIndex=1&pageSize=10",
type: "GET",
dataType: "jsonp", //指定服务器返回的数据类型
success: function (data) {
const filename = "file.xlsx"; //文件名称
const data1 = ['aWord', 'introduction', "listenTimes"]
var arrop = []
arrop.push(data1)
for (var i = 0; i < data.listRing.length; i++) {
let arr = []
arr.push(data.listRing[i].aWord)
arr.push(data.listRing[i].introduction)
arr.push(data.listRing[i].ringName)
arrop.push(arr)
}
// 数据, 一定注意需要时二维数组
var ws_name = "Sheet1"; //Excel第一个sheet的名称
var wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(arrop);
XLSX.utils.book_append_sheet(wb, ws, ws_name); //将数据添加到工作薄
XLSX.writeFile(wb, filename); //导出Excel
}
});
}
</script>
```