vue+elementui表格导出

htmlToExcel.js

import FileSaver from 'file-saver'
import XLSX from 'xlsx'

const htmlToExcel = {
  getExcel(dom, title = '默认标题') {
    var excelTitle = title
    var wb = XLSX.utils.table_to_book(document.querySelector(dom))
    /* 获取二进制字符串作为输出 */
    var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
    try {
      FileSaver.saveAs(
        new Blob([wbout], { type: 'application/octet-stream' }),
        excelTitle + '.xlsx'
      )
    } catch (e) {
      if (typeof console !== 'undefined') console.log(e, wbout)
    }
    return wbout
  }
}

export default htmlToExcel


<template>
  <div>
    <!--导出按钮-->
    <el-button type="primary" style="margin:20px;" @click="exportExcelSelect">导出Excel</el-button>
    <el-input
      style="width: 200px"
      placeholder="请输入搜索内容"
      v-model="inputSearch"
    >
      <i slot="prefix" class="el-input__icon el-icon-search" @click="search()"></i>
    </el-input>
    <!--原始表格-->
    <el-table
      :data="tableData"
      height="500px"
      fixed="left"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
      >
      </el-table-column>
      <el-table-column
        prop="helpKeywordId"
        label="helpKeywordId"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="name"
      >
      </el-table-column>
    </el-table>
    <!--预览弹窗表格-->
    <el-dialog title="表格保存预览" width="70%" :visible.sync="selectWindow">
      <el-table :data="selectData" id="selectTable" height="380px">
        <el-table-column
          prop="helpKeywordId"
          label="helpKeywordId"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="name"
        >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exportExcel">确定保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import htmlToExcel from '@/utils/htmlToExcel'
import axios from 'axios'

export default {
  name: 'ExcelPage',
  data() {
    return {
      inputSearch: '',
      // 表格数据
      tableData: null,
      // 表格中选中的数据
      selectData: [],
      selectWindow: false
    }
  },
  methods: {
    // 导出
    exportExcel() {
      htmlToExcel.getExcel('#selectTable', '导出的自定义标题')
    },
    // 显示预览弹窗
    exportExcelSelect() {
      if (this.selectData.length < 1) {
        this.$message.error('请选择要导出的内容!')
        return false
      }
      this.selectWindow = true
    },
    // 选中数据
    handleSelectionChange(val) {
      this.selectData = val
    },
    search() {
    }
  },
  created() {
    axios.get('http://localhost:9090/findAll').then((response) => {
      this.tableData = response.data
      console.log(response.data)
    })
  }
}
</script>

<style>
</style>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值