美文网首页
vue中将el-table导出为.xlsx的excel表格

vue中将el-table导出为.xlsx的excel表格

作者: leesession | 来源:发表于2019-11-14 14:41 被阅读0次

1.引入2个必要的包

npm i xlsx file-saver -S

2.在需要导出表格的位置,引入这2个包

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

3.对el-table加上id属性,注:作者试了ref,不能用

<el-table
     id="mytable"
     :data="userData"
     border>

4.设置导出的事件行为

 <el-button @click="output" >导出</el-button>

5.写output方法,直接复制就行了

output(){
        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" }),
            "sheet1.xlsx"//导出的表名
          );
        } catch (e) {
          if (typeof console !== "undefined") console.log(e, wbout);
        }
        return wbout;
      }

相关文章

网友评论

      本文标题:vue中将el-table导出为.xlsx的excel表格

      本文链接:https://www.haomeiwen.com/subject/wiouaqtx.html