将el-table中的数据导出到excel中

定义一个exportAllToExcel函数来实现导出

import * as XLSX from 'xlsx'
import FileSaver from 'file-saver'
// 导出所有数据
export function exportAllToExcel(_this, getData, fileName, tableId) {
  const temp_pageNo = _this.query.pageNo
  const temp_pageSize = _this.query.pageSize
  const temp_oprationShow = _this.showOperation
  _this.showOperation = false // 不显示操作按钮
  _this.query.pageNo = 1 // 显示第一页
  _this.query.pageSize = _this.total // 将表格长度变成数据总长度
  getData(_this.query).then((res) => {
    _this.$nextTick(() => {
      const wb = XLSX.utils.table_to_book(document.querySelector(tableId ? `#${tableId}` : '#out-table'))
      const wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array',
      })
      try {
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream',
          }),
          fileName + '.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      _this.query.pageNo = temp_pageNo
      _this.query.pageSize = temp_pageSize
      _this.showOperation = temp_oprationShow
      return wbout
    })
  })
}

其中getData为请求数据列表的API接口,fileName为导出文件的名字,tableId为要导出table的id

 <el-table
                :data="rightTableData"
                row-key="id"
                size="small"
                height="calc(100% - 10px)"
                stripe
                ref="table"
                id="out-table"
              ></el-table>

如果只想要导出本页面的数据,则不需要改变this.query.pageSize的值。

导出效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值