导入XLSX库
官方地址Github
安装
npm install xlsx --s
简单二次封装js-xlsx库导入导出功能
封装为excel.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
import XLSX from 'xlsx';
function exportExcel(json, name) { var data = new Array(); var keyArray = new Array();
for (const key1 in json) { if (json.hasOwnProperty(key1)) { const element = json[key1]; var rowDataArray = new Array(); for (const key2 in element) { if (element.hasOwnProperty(key2)) { const element2 = element[key2]; rowDataArray.push(element2); if (keyArray.length < getLength(element)) { keyArray.push(key2); } } } data.push(rowDataArray); } } data.splice(0, 0, keyArray); const ws = XLSX.utils.aoa_to_sheet(data); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "SheetJS"); XLSX.writeFile(wb, name + ".xlsx"); }
function importExcel(callback) { var inputObj = document.createElement('input') inputObj.setAttribute('id', 'file'); inputObj.setAttribute('type', 'file'); inputObj.setAttribute('name', 'file'); inputObj.setAttribute("style", 'visibility:hidden'); inputObj.setAttribute("accept", ".xlsx,.xls,.csv") inputObj.addEventListener('change', (evt) => { const files = evt.target.files; if (files && files[0]) _file(files[0], (data, dataRef) => { callback(data, dataRef) }); }) document.body.appendChild(inputObj); inputObj.value; inputObj.click();
}
function _file(file, callback) { const make_cols = refstr => Array(XLSX.utils.decode_range(refstr).e.c + 1).fill(0).map((x, i) => ({ name: XLSX.utils.encode_col(i), key: i }));
const reader = new FileReader(); reader.onload = (e) => { const bstr = e.target.result; const wb = XLSX.read(bstr, { type: 'binary' }); const wsname = wb.SheetNames[0]; const ws = wb.Sheets[wsname]; const data = XLSX.utils.sheet_to_json(ws, { header: 1 }); callback(data, make_cols(ws['!ref']))
}; reader.readAsBinaryString(file); }
function getLength(obj) {
var count = 0; for (var i in obj) {
if (obj.hasOwnProperty(i)) { count++;
} }
return count; } export default { exportExcel, importExcel }
|
exportExcel 导出文件,importExcel 导入文件,参数说明看代码中注释
在其他文件使用封装的js
在App.vue中
首先import文件
import Excel from "./libs/excel.js";
示例导出的json对象,写在data中
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { name: "路人甲", phone: "123456789", email: "000@123456.com" }, { name: "炮灰乙", phone: "123456789", email: "000@123456.com" }, { name: "土匪丙", phone: "123456789", email: "000@123456.com" }, { name: "流氓丁", phone: "123456789", email: "000@123456.com" } ]
|
method中写对应的方法
1 2 3 4 5 6 7 8 9 10
| exportExcel() { Excel.exportExcel(this.jsonData, "文件"); }, importFile() { Excel.importExcel((data, dataRef) => { alert("数据已经打印在控制台"); console.log(data); console.log(dataRef); }); }
|
太简单了没写demo,伸手党请不要来问demo。有疑问请留意。