vue中elementUI表格数据导出下载为xlsx格式的excel文件

转载bamboozjy原文链接
1.安装依赖:

npm i file-saver --save
npm i xlsx --save

2.在需要的地方引入这两个插件:

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

3.在点击事件中导出和数据:mytable为表格的id名,sheet.xlsx为导出的文件名
html:

<el-table
    id="mytable"
    ref="multipleTable"
    :data="user.slice((currentPage-1)*pagesize,currentPage*pagesize)"
    tooltip-effect="dark"
    style="width: 100%"
    @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="user_id" label="序号"></el-table-column>
        <el-table-column prop="user_nickname" label="微信昵称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="user_name" label="姓名"></el-table-column>
        <el-table-column prop="userSex" label="性别"></el-table-column>
</el-table>
 
<button @click="output">导出数据</button>

js:

output() {
      /* generate workbook object from table */
      var wb = XLSX.utils.table_to_book(document.querySelector("#mytable"));//mytable为表格的id名
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          "sheet.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值