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>
相关推荐
程序员黑豆2 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC2 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea2 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
浮生望3 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少3 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
波波0073 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis4 小时前
es6知识点1-自备使用
前端·ecmascript·es6
小爬的老粉丝5 小时前
JavaScript 纯前端预览 WPS:先识别容器,再路由解析器
前端·javascript·wps
计算机魔术师6 小时前
新兴多智能体系统的模式与问题
前端
用户059540174466 小时前
AI Agent 上下文污染踩坑实录:用 pytest + Redis 揪出 3 类记忆串号 bug,排查 6 小时
前端·css