import * as xlsx from "COMMON_NODE_MODULES/xlsx";
headers: [],
data: [],
beforeUpload(file) {
const isExcel =
file.type ===
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
file.type === "application/vnd.ms-excel";
console.log(isExcel, "isExcel");
if (!isExcel) {
this.$Message.warning("只能上传 .xlsx 或者.xls 格式的文件");
return false;
}
this.fileList = file;
const reader = new FileReader();
reader.onload = (e) => {
console.log(e, "load");
const data = new Uint8Array(e.target.result);
const workbook = xlsx.read(data, { type: "array" });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const json = xlsx.utils.sheet_to_json(worksheet, { header: 1 });
this.data = json.slice(1);
console.log(this.data, "333333333");
};
reader.readAsArrayBuffer(file);
return false;
},
confirm() {
if (!this.fileList) {
this.$message.error("请上传文件");
return;
}
console.log(this.data, "this.data");
const promises = this.data.map((item) => {
return this.getAdd(item).then(() => {});
});
Promise.all(promises)
.then(() => {
this.loading = false;
this.$Message.success("导入成功");
this.cancel();
this.$emit("importResh");
})
.catch((error) => {
console.error("Error processing items:", error);
});
},