npm i xlsx -S
import { read, utils } from "xlsx"
<el-upload class="upload-demo"
action="https://jsonplaceholder.typicode.com/posts/"
accept='.xls,.xlsx'
:on-success="handleChange"
:show-file-list='false'
:file-list="fileList">
<el-button size="small"
type="primary">导入EXCEL</el-button>
</el-upload>
handleChange(res, file, fileList) {
for (let i = 0; i < fileList.length; i++) {
if (file.name != fileList[i].name) {
this.fileList.push({
name: file.name,
url: "",
uid: file.uid
});
}
}
const files = { 0: file };
this.readExcel(files);
},
readExcel(file) {
const fileReader = new FileReader();
fileReader.onload = ev => {
try {
const data = ev.target.result;
const workbook = read(data, { type: "binary" });
const params = [];
workbook.SheetNames.forEach(item => {
this.exceltableData.push(utils.sheet_to_json(workbook.Sheets[item]));
});
if (this.exceltableData.length > 0) {
for (const key in this.exceltableData[0][0]) {
this.tableHead.push(key);
}
}
console.log(this.exceltableData[0])
} catch (e) {
console.log("error:" + e);
return false;
}
};
fileReader.readAsBinaryString(file[0].raw);
}