导出PDF,Excel

1.下载插件

npm i exceljs@4.3.0
npm i  jspdf@2.5.1

2.引入插件

import Excel from "exceljs";
import jsPDF from "jspdf";

3.先说Excel

// 获取计划进度
    async PlanGetExportPlanData(type) {
      const res = await this.$api.PlanGetExportPlanData(this.planDataForm);
      if (res.success) {
        this.exportVisible = false;
        if (type == "Excel") {
          const workbook = new Excel.Workbook();
          const worksheet = workbook.addWorksheet("Sheet1");
          worksheet.columns = [
            { header: "学员姓名", key: "realName", width: 20 },
            { header: "身份证号", key: "idCard", width: 20 },
            { header: "手机号", key: "phone", width: 20 },
            { header: "工作单位", key: "workUnit", width: 20 },
            { header: "计划名称", key: "planName", width: 20 },
            { header: "最后学习时间", key: "lastTime", width: 20 },
            { header: "学习总时长", key: "totalStudyTime", width: 20 },
            { header: "总学时", key: "study_hour", width: 20 },
            { header: "完成学时", key: "finish_study_hour", width: 20 },
            { header: "完成进度", key: "finishSchedule", width: 20 },
            { header: "开始时间", key: "startTime", width: 20 },
            { header: "结束时间", key: "endTime", width: 20 },
          ];
          const data = res.response.dataList;
          data.forEach((item) => {
            worksheet.addRow(item);
          });
          workbook.xlsx.writeBuffer().then((buffer) => {
            const blob = new Blob([buffer], {
              type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            });
            const a = document.createElement("a");
            a.download = "学员进度.xlsx";
            a.href = URL.createObjectURL(blob);
            a.click();
          });
        } else if (type == "PDF") {
          this.exportUserType = 'PDF'
          this.exportPdfDataList = res.response;
          this.getBase64(this.exportPdfDataList.stampLink);
        }
      }
    },
当连接接口时调用,
@click="PlanGetExportPlanData('Excel')"
当调用Excel格式时传入,下面接收

4.PDF

    getBase64(imgUrl) {
      let that = this;
      window.URL = window.URL || window.webkitURL;
      var xhr = new XMLHttpRequest();
      xhr.open("get", imgUrl, true);
      xhr.responseType = "blob";
      xhr.onload = function () {
        if (this.status == 200) {
          var blob = this.response;
          let oFileReader = new FileReader();
          oFileReader.onloadend = function (e) {
            that.imgSrc = e.target.result;
          };
          oFileReader.readAsDataURL(blob);
        }
      };
      xhr.send();
    },

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值