Front-end vue+elementui exports complex (cell merging, multi-level headers) table el-table to excel export

Requirement: Front-end export of el-table table

report

npm install xlsx -S

npm install file-saver --save

Principle: Directly export the data in the el-table table. This will have the disadvantage that only the data on the current page will be exported. If you need to export all the data, you can re-export it yourself. Render an el-table with all data invisible and export it

Extension: After testing, not only el-table tables can be exported, but also tables of various ui components, such as vxe-table, vant and other component tables. It can also be exported, as long as the tables <tr>, <th> and other tags can be exported

<template>
<div>
  <el-table
    ref="report-table"
    :data="tableData"
    style="width: 100%">
    <el-table-column
      prop="date"
      label="日期"
      width="150">
    </el-table-column>
    <!-- 各种自定义内容,单元格合并,多级表头  -->

  </el-table>
  <el-button @click="exportExcel('导出')">导出</el-button>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  name: 'project',
  data() {
      return {
        tableData: [{
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }]
      }
    },
  components: {

  },
  watch: {

  },
  methods: {
    exportExcel(excelName) {
      try {
        //获取表格
        const $e = this.$refs['report-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if(!$table) {
          $table = $e
        }

        const wb = XLSX.utils.table_to_book($table, {raw:true})
        const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST:true, type: 'array'})
        FileSaver.saveAs(
          new Blob([wbout],{type: 'application/octet-stream'}),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  },
  mounted () {
  }
}
</script>
<style>

</style>

Problem: The exported Excel table data is duplicated. The reason is that the fixed attribute of el-table makes a certain column fixed

exportExcel() {
      var xlsxParam = { raw: true }; // 导出的内容只做解析,不进行格式转换
      let fix = document.querySelector(".el-table__fixed");//如果是都给了fixed样式
      let fix = document.querySelector(".el-table__fixed-right");//如果是只有右边有fixed样式
      let wb;
      if (fix) {
        //判断要导出的节点中是否有fixed的表格,如果有,转换excel时先将该dom移除,然后append回去
        wb = XLSX.utils.table_to_book(
          document.querySelector("#educe-table").removeChild(fix),
          xlsxParam
        );
        document.querySelector("#educe-table").appendChild(fix);
      } else {
        wb = XLSX.utils.table_to_book(
          document.querySelector("#educe-table"),
          xlsxParam
        );
      }
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          "商户列表.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") {
        }
      }
      return wbout;
    },

sheet

 Exported excel file

Guess you like

Origin blog.csdn.net/MadSnail00/article/details/131845262