vue+elementui表格导出

htmlToExcel.js

javascript 复制代码
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>
相关推荐
BillKu1 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
郝YH是人间理想2 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core2 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情2 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287562 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔2 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好3 小时前
HTML 表格与div深度解析区别及常见误区
前端·html
运维@小兵3 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js
koiy.cc3 小时前
记录:echarts实现tooltip的某个数据常显和恢复
前端·echarts
一只专注api接口开发的技术猿4 小时前
企业级电商数据对接:1688 商品详情 API 接口开发与优化实践
大数据·前端·爬虫