action 必传,可传空
npm i xlsx
beforeImport(file) {
console.log(file);
this.file = file;
this.readExcel();
},
readExcel(e) {
//表格导入
// var that = this;
// const files = e.target.files[0];
const files = this.file;
console.log(files);
// if (files.length <= 0) {
// //如果没有文件名
// return false;
// } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
// this.$Message.error("上传格式不正确,请上传xls或者xlsx格式");
// return false;
// }
const fileReader = new FileReader();
fileReader.onload = (ev) => {
try {
const data = ev.target.result;
const workbook = XLSX.read(data, {
type: "binary",
});
const list = workbook.SheetNames[0]; //取第一张表
const pL = XLSX.utils.sheet_to_json(workbook.Sheets[list]); //生成json表格内容
console.log(pL);
this.importData = {}; //清空接收数据
// //编辑数据
// for (var i = 0; i < ws.length; i++) {
// var sheetData = {
// address: ws[i].addr,
// value: ws[i].value,
// };
// this.xlsxArr.push(sheetData);
// }
// this.$refs.upload.value = "";
} catch (e) {
console.log(e);
return false;
}
};
fileReader.readAsBinaryString(files);
},