Bootstrap

vue 导出excel文件 利用 excelJs插件

用excelJs导出excel文件 没有咋整理发布出来是怕自己忘了咋写

	exportExcel() {
      if (this.data.length == 0) {
        this.$message.error("暂无数据");
        return;
      }
      var ExcelJS = require("exceljs");
    
      // 创建工作簿
      var workbook = new ExcelJS.Workbook();
      // 创建工作表
      const sheet = workbook.addWorksheet("Sheet1");
      // 创建表头
     
      let headercolum = [
        {
          header: `专家`,
          key: "customerFullName",
          width: 30,
        },
      ];
      let endcol = [
        {
          header: "专家类型",
          key: "expertsType",
          width: 20,
        },
        {
          header: "专业",
          key: "expertsMajor",
          width: 20,
        },
        {
          header: "姓名",
          key: "personName",
          width: 20,
        },
        {
          header: "手机",
          key: "mobilephone",
          width: 20,
        },
        {
          header: "座机",
          key: "telephone",
          width: 20,
        },
        {
          header: "状态",
          key: "lastLocation",
          width: 20,
        },
        {
          header: "位置",
          key: "remarks",
          width: 50,
        },
        {
          header: "打卡时间",
          key: "lastTime",
          width: 20,
        },
      ];

      sheet.columns = [...endcol];

      let dataSource = [...this.data];
      dataSource.map(el =>{
        el.lastTime = el.lastTime == undefined ?  ''  : moment(el.lastTime).format('YYYY-MM-DD HH:mm:ss')

      })
      dataSource.forEach((item, index) => {
        sheet.addRow({ ...item });
      });

      // sheet.mergeCells(`A1:A${num}`);
      // sheet.mergeCells("A1:C1");
      // sheet.mergeCells(`A1:B1`);
      // sheet.mergeCells(`A2:A3`);

      // 设置每一行居中
      // const row = sheet.getRow(1);
      sheet.eachRow((row, rowNumber) => {
        sheet.getRow(rowNumber).alignment = {
          vertical: "middle",
          horizontal: "center",
          wrapText: "true",
        };
        // 首行字体18,其余行12
        sheet.getRow(rowNumber).font = {
          size:
            rowNumber === 1 ? 21 : rowNumber === 2 || rowNumber === 3 ? 12 : 12,
        };
        // 遍历一行中的所有单元格(包括空单元格)
        row.eachCell({ includeEmpty: true }, (cell, colNumber) => {
          cell.border = {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          };
        });
      });
      sheet.horizontalCentered = true;
      // 下载 excel
      workbook.xlsx.writeBuffer().then((buf) => {
        let blob = new Blob([buf], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
        });
        const downloadElement = document.createElement("a");
        let href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        downloadElement.download = `井控专家.xlsx`; // 文件名字
        document.body.appendChild(downloadElement);
        downloadElement.click();
        document.body.removeChild(downloadElement); // 下载完成移除元素
        window.URL.revokeObjectURL(href); // 释放掉blob对象
      });
    }
;