1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import htmlTocanvas from "html2canvas"; import ExcelJS from "exceljs"; import jsPDF from "jspdf"; import { saveAs } from "file-saver"; export const ExportUtil = {
exportExcel({ name = `${new Date().getTime()}`, data = [], columns = [], mergeColumns = [], waterText = true, }) { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet("sheet1"); if (waterText) { const defaultText = "测试水印"; waterText = typeof waterText === "string" ? waterText : defaultText; const base64Img = this.setWatermark(waterText); const img = workbook.addImage({ base64: base64Img, extension: "png", }); worksheet.addBackgroundImage(img); }
if (columns && columns.length) { const columnsData = columns.map((column) => { return { header: column.title, key: column.dataIndex || column.field, style: { alignment: { horizontal: "center", vertical: "middle" }, }, width: 20, }; }); worksheet.columns = columnsData; } if (mergeColumns && mergeColumns.length) { mergeColumns.forEach((item) => { const start = item.mergeCells.split(":")[0]; worksheet.mergeCells(item.mergeCells); worksheet.getCell(start).value = item.value; worksheet.getCell(start).alignment = { horizontal: "center", vertical: "middle", }; worksheet.getCell(start).font = { bold: true }; }); }
worksheet.addRows(data); workbook.xlsx.writeBuffer().then((res) => { saveAs( new Blob([res], { type: "application/octet-stream" }), name.includes(".xlsx") ? name : `${name}.xlsx` ); }); }, exportPDF({ element, fileName, config = {}, waterText = true }) { return new Promise((resolve, reject) => { htmlTocanvas(element, { useCORS: true, backgroundColor: "#f7f7f8", ...config, }) .then((canvas) => { let base64Img; if (waterText) { const userInfo = store.getters.userInfo; const defaultText = "测试水印"; waterText = typeof waterText === "string" ? waterText : defaultText; base64Img = this.setWatermark(waterText, canvas); } const imgData = base64Img || canvas.toDataURL("image/png"); const pdf = new jsPDF("p", "pt", "a4"); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgWidth = pdfWidth; const imgHeight = (imgWidth * pdfHeight) / pdfWidth; pdf.addImage(imgData, "PNG", 0, 0, imgWidth, imgHeight); pdf.save(fileName.includes(".pdf") ? fileName : `${fileName}.pdf`); resolve(); }) .catch((err) => { reject(err); }); }); },
exportImg({ element, fileName, config = {}, waterText = true }) { return new Promise((resolve, reject) => { htmlTocanvas(element, { useCORS: true, backgroundColor: "#f7f7f8", ...config, }) .then((canvas) => { let base64Img; if (waterText) { const defaultText = "测试水印"; waterText = typeof waterText === "string" ? waterText : defaultText; base64Img = this.setWatermark(waterText, canvas); } const link = document.createElement("a"); link.download = `${fileName || new Date().getTime()}.png`; link.href = base64Img || canvas.toDataURL("image/png"); link.click(); resolve(); }) .catch((err) => { reject(err); }); }); },
setWatermark(str, canvas) { let can = canvas || document.createElement("canvas"); if (!canvas) { can.width = 500; can.height = 400; } let ctx = can.getContext("2d"); ctx.font = "18px Vedana"; ctx.fillStyle = "rgba(130, 142, 162, 0.5)"; ctx.rotate((-60 * Math.PI) / 220); ctx.textAlign = "left"; ctx.textBaseline = "Middle";
if (canvas) { const xOffset = -can.width / 2; const yOffset = -can.height / 2;
for (let x = 0; x < can.width * 1.5; x += 400) { for (let y = 0; y < can.height * 1.5; y += 300) { ctx.fillText(str, x + xOffset, y + yOffset); } } } else { let initY = can.height; ctx.fillText(str, 0, initY); }
return can.toDataURL("image/png"); }, };
|