Vue导出复杂excel表格(适用于ElementUI、Iview)

示图:

 

1.安装依赖

npm install --save xlsx file-saver

2.引入依赖到当前文件中

import FileSaver from "file-saver";
import XLSX from "xlsx";

3.导出事件代码

exportExcel(excelName) {
      try {
        const $e = this.$refs['tabel'].$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)
      }
},

4.完整代码如下(我用的是iview框架,elementui方法一样):

<template>
  <div class="tabel">
    <div style="text-align: left">
      <Button class="btn-style" @click="exportExcel(123)">导出</Button>
    </div>
    <div class="tabel-con">
      <Table
        :columns="columns11"
        ref="tabel"
        :data="data10"
        border
        height="500"
      ></Table>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import FileSaver from "file-saver";
import XLSX from "xlsx";

export default {
  name: "TabelIview",
  components: {},
  data() {
    return {
      columns11: [
        {
          title: 'Name',
          key: 'name',
          align: 'center',
          width: 200,
          fixed: 'left',
          filters: [
            {
              label: 'Joe',
              value: 1
            },
            {
              label: 'John',
              value: 2
            }
          ],
          filterMultiple: false,
          filterMethod (value, row) {
            if (value === 1) {
              return row.name === 'Joe';
            } else if (value === 2) {
              return row.name === 'John Brown';
            }
          }
        },
        {
          title: 'Other',
          align: 'center',
          children: [
            {
              title: 'Age',
              key: 'age',
              align: 'center',
              width: 200,
              sortable: true
            },
            {
              title: 'Address',
              align: 'center',
              children: [
                {
                  title: 'Street',
                  key: 'street',
                  align: 'center',
                  width: 200
                },
                {
                  title: 'Block',
                  align: 'center',
                  children: [
                    {
                      title: 'Building',
                      key: 'building',
                      align: 'center',
                      width: 200,
                      sortable: true
                    },
                    {
                      title: 'Door No.',
                      key: 'door',
                      align: 'center',
                      width: 200
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          title: 'Company',
          align: 'center',
          children: [
            {
              title: 'Company Address',
              key: 'caddress',
              align: 'center',
              width: 200
            },
            {
              title: 'Company Name',
              key: 'cname',
              align: 'center',
              width: 200
            }
          ]
        },
        {
          title: 'Gender',
          key: 'gender',
          align: 'center',
          width: 200,
          fixed: 'right'
        }
      ],
      data10: []
    };
  },
  methods: {
    exportExcel(excelName) {
      try {
        const $e = this.$refs['tabel'].$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() {
    this.tableHeight = window.innerHeight - this.$refs.tabel.$el.offsetTop - 50;
    const data = [];
    for (let i = 0; i < 20; i++) {
      data.push({
        key: i,
        name: 'John Brown',
        age: i + 1,
        street: 'Lake Park',
        building: 'C',
        door: 2035,
        caddress: 'Lake Street 42',
        cname: 'SoftLake Co',
        gender: 'M',
      });
    }
    this.data10 = data;
  },
};
</script>

<style lang="less" scoped>
.tabel {
  width: 100%;
  height: calc(100vh - 60px);
  overflow: auto;
  padding: 20px 50px;
  box-sizing: border-box;
  .tabel-con {
    width: 100%;
    height: calc(100vh - 150px);
    padding-top: 15px;
    box-sizing: border-box;
  }
}
</style>

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值