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();
},