el-table样式

导出的excel样式

示例代码
<template>
<el-button type="primary" @click="onClick"> 导出 </el-button>
<el-table :data="tableData" style="width: 100%" border ref="ERef">
<el-table-column label="歌手信息大全" align="center">
<el-table-column prop="id" label="ID" width="80"></el-table-column>
<el-table-column prop="name" label="歌手"></el-table-column>
<el-table-column prop="region" label="住址"></el-table-column>
</el-table-column>
</el-table>
</template>
<script setup>
import { utils } from "xlsx";
import * as XLSX_STYLE from "xlsx-style-vite";
import FileSaver from "file-saver";
const ERef = ref(null);
const tableData = ref([
{ id: 1, name: "周杰伦", region: "北京王府井文华东方酒店" },
{ id: 2, name: "林俊杰", region: "上海东方明珠" },
{ id: 3, name: "", region: "广州塔" },
{},
]);
function getStrWidth(text, fontSize) {
const span = document.createElement("span");
const styleObj = {
font: "bold",
fontSize: fontSize + "px",
visibility: "hidden",
whiteSpace: "pre",
};
Object.assign(span.style, styleObj);
span.textContent = text;
document.body.appendChild(span);
const width = span.offsetWidth;
document.body.removeChild(span);
return width;
}
const onClick = () => {
exportElTable(ERef.value, {
errorCells: { 2: [2] },
hasExtraHeader: true,
});
};
function exportElTable(ERef, option = {}) {
if (!ERef) return;
const {
filename = "fileName.xlsx",
errorCells = {},
headerRowCnt = 1,
hasExtraHeader = false,
firstColBold = false,
} = option;
const { $el } = ERef;
const wb = utils.book_new();
const headerDOM = $el.querySelector(
".el-table .el-table__header-wrapper .el-table__header"
);
const bodyDOM = $el.querySelector(
".el-table .el-table__body-wrapper .el-table__body"
);
let divEle = document.createElement("div");
if (headerDOM) {
divEle.appendChild(headerDOM.cloneNode(true));
}
divEle.appendChild(bodyDOM.cloneNode(true));
const sheet = utils.table_to_sheet(divEle, { raw: true });
const colWidths = [];
const range = utils.decode_range(sheet["!ref"]);
const borderStyle = { style: "thin", color: { rgb: "000000" } };
const getBorderStyleObj = (direction, cell) => {
const obj = cell || { s: { border: {} } };
obj.s.border[direction] = borderStyle;
return obj;
};
const getCellRef = (r, c) => utils.encode_cell({ r, c });
const getCell = (r, c) => sheet[getCellRef(r, c)];
const addBorder = (R, C, dir) => {
const CellRef = getCellRef(R, C);
sheet[CellRef] = getBorderStyleObj(dir, sheet[CellRef]);
};
for (let C = range.s.c; C <= range.e.c; ++C) {
let maxWidth = 60;
for (let R = range.s.r; R <= range.e.r; ++R) {
const cell = getCell(R, C);
if (!cell?.t) {
if (hasExtraHeader && R == 0 && C > 0) {
addBorder(R, C, "bottom");
}
if (R == range.e.r) {
addBorder(R, C, "bottom");
} else if (R > 0) {
const topCell = getCell(R - 1, C);
const bottomCell = getCell(R + 1, C);
if (!topCell?.t && bottomCell?.t) {
addBorder(R, C, "top");
}
}
if (C == range.e.c) {
addBorder(R, C, "right");
} else if (C > 0) {
const leftCell = getCell(R, C - 1);
const rightCell = getCell(R, C + 1);
if (!leftCell?.t && rightCell?.t) {
addBorder(R, C, "left");
}
}
continue;
}
const cellStyle = {
border: {
top: borderStyle,
bottom: borderStyle,
left: borderStyle,
right: borderStyle,
},
alignment: { horizontal: "center", vertical: "center" },
};
if (C == 0 && firstColBold) {
cellStyle.font = { bold: true };
}
if (R < headerRowCnt) {
cellStyle.font = {
bold: true,
...(hasExtraHeader && R === 0 ? { sz: 18 } : {}),
};
}
if (errorCells[R]?.includes(C)) {
cellStyle.font = { color: { rgb: "FF0000" } };
}
cell.s = cellStyle;
if (cell.v && !(hasExtraHeader && R == 0)) {
const cellValue = cell.v.toString();
const fontSize = C == range.e.c ? 16 : 14;
maxWidth = Math.max(maxWidth, getStrWidth(cellValue, fontSize));
}
}
colWidths.push({ wpx: maxWidth });
}
sheet["!cols"] = colWidths;
utils.book_append_sheet(wb, sheet);
divEle = null;
const s2ab = (s) => {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i < s.length; i++) {
view[i] = s.charCodeAt(i) & 0xff;
}
return buf;
};
const writeOption = {
bookType: "xlsx",
bookSST: false,
type: "binary",
cellStyles: true,
};
const wbout = XLSX_STYLE.write(wb, writeOption);
try {
FileSaver.saveAs(
new Blob([s2ab(wbout)], {
type: 'application/octet-stream;charset=utf-8"',
}),
filename
);
} catch (e) {
if (typeof console !== "undefined") {
}
}
}
</script>
<style scoped lang="scss"></style>