报错:~~~~
子组件:
impotEvent
方法导入excel
<div class="table-box">
<vxe-toolbar>
<template v-slot:buttons>
<vxe-button @click="impotEvent">选择文件</vxe-button>
</template>
</vxe-toolbar>
<vxe-table
border
resizable
show-overflow
keep-source
ref="xTable"
width="100%"
height="auto"
max-height="600"
class="xTable"
:loading="loading"
:data="dataSource"
>
<vxe-table-column
v-for="(item, i) in tableColumn"
:key="i"
:type="item.type"
:title="item.title"
:field="item.field"
:fixed="item.fixed"
:width="item.width"
:min-width="item.mWidth"
></vxe-table-column>
</vxe-table>
</div>
<script>
import XLSX from "xlsx";
export default {
props: ["tableColumn"],
data() {
return {
loading: false, // table蒙层
dataSource: [],
};
},
methods: {
impotEvent() {
this.$refs.xTable
.readFile({
types: ["xls", "xlsx"]
})
.then(params => {
// console.log(params);
const { files } = params;
const fileReader = new FileReader();
fileReader.onload = ev => {
const data = ev.target.result;
const workbook = XLSX.read(data, { type: "binary" });
const csvData = XLSX.utils.sheet_to_csv(workbook.Sheets.Sheet1);
const tableData = [];
// 解析数据
csvData.split("
").forEach(vRow => {
if (vRow) {
const vCols = vRow.split(",");
const item = {};
vCols.forEach((val, cIndex) => {
const column = this.tableColumn[cIndex];
console.log(column.field) // 打印出来了,但是 .field 一直会报错
if(column.field != undefined) {
item[column.field] = val;
// console.log(item)
}
});
tableData.push(item);
}
});
console.log(tableData)
this.dataSource = tableData;
};
fileReader.readAsBinaryString(files[0]);
});
}
}
};
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…