vue 导出为表格

1,终端运行

cnpm install --save xlsx file-saver

2,Script部分

import FileSaver from "file-saver";
import XLSX from "xlsx";
exportexcel() {
         //设置当前日期
      let time = new Date();
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      let name = year + "" + month + "" + day;
      //console.log(name)
      //选择要导出的表格
      var wb = XLSX.utils.table_to_book(document.querySelector(".table"));
      //console.log(wb)
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      //console.log(wbout)
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          name + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

借鉴来源:https://blog.csdn.net/weixin_42429288/article/details/84140654

posted @ 2020-03-18 22:29  学习就是进步!  阅读(301)  评论(0)    收藏  举报