(此內容轉自https://blog.csdn.net/qq_38502227/article/details/101702819)
//util.jsimport"@/utils/excel/Blob";export{ json2excel, getCharCol, formatJson };// function parseTime(time, cFormat) {// if (arguments.length === 0) {// return null;// }// const format = cFormat || "{y}-{m}-ze8trgl8bvbq {h}:{i}:{s}";// let date;// if (typeof time === "object") {// date = time;// } else {// if (typeof time === "string" && /^[0-9]+$/.test(time)) {// time = parseInt(time);// }// if (typeof time === "number" && time.toString().length === 10) {// time = time * 1000;// }// date = new Date(time);// }// const formatObj = {// y: date.getFullYear(),// m: date.getMonth() + 1,// d: date.getDate(),// h: date.getHours(),// i: date.getMinutes(),// s: date.getSeconds(),// a: date.getDay()// };// // eslint-disable-next-line// const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {// let value = formatObj[key];// // Note: getDay() returns 0 on Sunday// if (key === "a") {// return ["日", "一", "二", "三", "四", "五", "六"][value];// }// if (result.length > 0 && value < 10) {// value = "0" + value;// }// return value || 0;// });// // eslint-disable-next-line// return time_str;// }functionjson2excel(tableJson, filenames, autowidth, bookTypes){import("@/utils/excel/Export2Excel").then(excel=>{var tHeader =[];var dataArr =[];var sheetnames =[];for(var i in tableJson){tHeader.push(tableJson[i].tHeader);dataArr.push(formatJson(tableJson[i].filterVal, tableJson[i].tableDatas));sheetnames.push(tableJson[i].sheetName);}excel.export_json_to_excel({header: tHeader,data: dataArr,sheetname: sheetnames,filename: filenames,autoWidth: autowidth,bookType: bookTypes});});}// 數據過濾,時間過濾functionformatJson(filterVal, jsonData){return jsonData.map(v=>filterVal.map(j=>{if(j ==="timestamp"){returnparseTime(v[j]);}else{return v[j];}}));}// 獲取26個英文字母用來表示excel的列functiongetCharCol(n){for(var i =0; i <this.list.length; i++){this.list[i].showActive =false;if(index == i){this.list[index].showActive =true;}}let temCol ="",s ="",m =0;while(n >0){m =(n %26)+1;s = String.fromCharCode(m +64)+ s;n =(n - m)/26;}return s;}
5.使用
<template><div class="home"><!--<img alt="Vue logo" src="../assets/logo.png">--><button @click="exportData">Excel導出</button></div></template><script>// @ is an alias to /src// import HelloWorld from '@/components/HelloWorld.vue'import{formatJson}from'@/utils/excel/excel'import{toExportExcel}from'@/utils/excel/Export2Excel'exportdefault{name:'Home',components:{// HelloWorld},methods:{exportData(){const list =[{name:'小明',age:16,},{name:'小紅',age:15,},{name:'小剛',age:16,},]const tHeader =['姓名','年齡']//表頭const filterVal =['name','age']//對應list'name',獲取對應'value'const data =formatJson(filterVal, list);const filename ="測試";toExportExcel(tHeader, data, filename);},}}</script>
效果如下圖: