```bash
main.js
import XLSX from 'xlsx'
Vue.prototype.XLSX = XLSX
事件执行
var list = [
{
id: 142,
uid: 12820382,
name: "",
crypto: "COPE",
amount: 16419380925822,
lockAmount: 0
}
]
let tableData = [
['id','uid', 'name','crypto', 'amount',"lockAmount"]//导出表头
]
// 表格表头
list.forEach ((item,index)=> {
let rowData = []
//导出内容的字段
rowData = [
item.id,
item.uid,
item.name,
item.crypto,
item.amount,
item.lockAmount
]
tableData.push(rowData)
})
let ws = this.XLSX.utils.aoa_to_sheet(tableData)
let wb = this.XLSX.utils.book_new()
this.XLSX.utils.book_append_sheet(wb, ws, '来访记录') // 工作簿名称
this.XLSX.writeFile(wb, 'xx.xlsx') // 保存的文件名
封装 事件
方法 引入 import plus from "@/components/export.js";
var list = [
{
id: 142,
uid: 12820382,
name: "",
crypto: "COPE",
amount: 16419380925822,
lockAmount: 0
}
]
let tableData = [
['id','uid', 'name','crypto', 'amount',"lockAmount"]//导出表头
]
let tableDatatilte =
['id','uid', 'name','crypto', 'amount',"lockAmount"]//导出字段
参数 数据 title 要取得字段
plus.a(list,tableData,tableDatatilte)
封装的export.js文件
import XLSX from 'xlsx'
import Vue from 'vue'
var elui = new Vue()
import XLSX from 'xlsx'
export default {
exportTable(list,tableData,tableDatatilte){
elui.$alert('是否导出全部数据?', '导出表格', {
confirmButtonText: '确定',
distinguishCancelAndClose:true,
callback: action => {
if(action=='confirm'){
this.tableData(list,tableData,tableDatatilte)
}else{
}
},
});
},
tableData(list,tableData,tableDatatilte) {
list.forEach ((item)=> {
let rowData = []
//导出内容的字段
for(let i=0;i<tableDatatilte.length;i++){
rowData.push( item[tableDatatilte[i]])
}
tableData.push(rowData)
})
console.log(tableData)
return
let ws = XLSX.utils.aoa_to_sheet(tableData)
let wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, '来访记录') // 工作簿名称
XLSX.writeFile(wb, 'xx.xlsx') // 保存的文件名
}
}
vue 导出 指定字段Excel 和封装
最新推荐文章于 2024-03-19 11:37:54 发布