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>
相关推荐
念念不忘 必有回响11 分钟前
nginx前端部署与Vite环境变量配置指南
前端·nginx·vite
JIngJaneIL39 分钟前
篮球论坛|基于SprinBoot+vue的篮球论坛系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·篮球论坛系统
程序猿阿伟2 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
麦麦大数据3 小时前
D030知识图谱科研文献论文推荐系统vue+django+Neo4j的知识图谱|论文本文相似度推荐|协同过滤
vue.js·爬虫·django·知识图谱·科研·论文文献·相似度推荐
fruge4 小时前
Vue Pinia 状态管理实战指南
前端·vue.js·ubuntu
绝无仅有4 小时前
某教育大厂面试题解析:MySQL索引、Redis缓存、Dubbo负载均衡等
vue.js·后端·面试
sean4 小时前
开发一个自己的 claude code
前端·后端·ai编程
用户21411832636024 小时前
dify案例分享-用 Dify 一键生成教学动画 HTML!AI 助力,3 分钟搞定专业级课件
前端
没逛够5 小时前
Vue 自适应高度表格
javascript·vue.js·elementui
太过平凡的小蚂蚁6 小时前
Kotlin 协程中常见的异步返回与控制方式(速览)
开发语言·前端·kotlin