Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

导入excel表格,数据打印出来了,但是会报undefined

报错:~~~~
image.png
子组件:
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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...