element ui 导出表格

2023-04-10 11:28:50

一、 安装到处Excel所需要的依赖包

npm install xlsx@^0.16.0 --save

npm install file-saver@^2.0.2 --save

二、在引用的页面中导入

import FileSaver from "file-saver";
import XLSX from "xlsx";

三、给elementuide table组件添加id

  <el-table 
        :data="tableDate" 
        id="out-table" 
        style="width:100%;"
    >
       <el-table-column 
          label="序号"
           prop="cardId"
          >
       </el-table-column>
   </el-table>

四、点击导出按钮

<el-button @click="exportExcel" type="primary" size="medium">导出</el-button>
 exportExcel () {
      var xlsxParam = { raw: true }
      var wb = XLSX.utils.table_to_book(
        document.querySelector('#table-out'),
        xlsxParam
      )
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          '用户个人信息.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },
  • 作者:slom_fxt
  • 原文链接:https://blog.csdn.net/slom_fxt/article/details/126995436
    更新时间:2023-04-10 11:28:50