file 是文件
_toJson(fiel) {
let self = this;
let file = fiel;
console.log(file);
let reader = new FileReader();
reader.onload = (event) => {
console.log(event);
let data = event.target.result;
const workbook = XLSX.read(data, {
type: "binary",
});
let resultArray = [];
let tableArray = [];
workbook.SheetNames.forEach(function (sheetName) {
console.log(sheetName);
console.log("workbook.Sheets[sheetName]", workbook.Sheets[sheetName]);
// XLSX.utils.sheet_to_json(worksheet , {defval:""})
const data = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {
header: 1,
defval: "",
});
console.log("data", data);
resultArray = [...data];
resultArray.splice(0, 2);
resultArray.forEach((item, index) => {
item = item.slice(0, 4);
console.log(item);
item[0] = String(item[0]).substring(0, 20);
let prefix = String(item[1]).substring(0, 2);
if (prefix == "黑龙") {
item[1] = String(item[1]).substring(0, 3);
} else {
item[1] = prefix;
}
if (item[1] === "内蒙") {
item[1] = "内蒙古";
}
item[2] = String(item[2]).substring(0, 10);
if (typeof item[3] == "number") {
console.log(typeof item[3]);
if (item[3] < 0) {
item[3] = "";
}else {
item[3] = Number(String(item[3]).substring(0, 10));
}
} else {
item[3] = "";
}
tableArray.push(
{
name: item[0],
province: item[1],
category: item[2],
revenue: item[3],
show_detail: 2,
image: { name: "", url: "" },
}
)
});
});
console.log(resultArray);
console.log(tableArray);
self.$emit("accept", tableArray);
};
reader.readAsBinaryString(file);
},
vue exale 转为 json
最新推荐文章于 2024-01-04 15:31:00 发布