testExcel(data) {
let userinfo = this.getuserName();
const workbook = new ExcelJS.Workbook();
// 创建带有红色标签颜色的工作表
// const sheet = workbook.addWorksheet("My Sheet", {
// properties: { tabColor: { argb: "FFC0000" } },
// });
// 创建一个第一行和列冻结的工作表
const sheet = workbook.addWorksheet("My Sheet");
sheet.properties.defaultColWidth = 30;
// 在表格底部追加新行
let wopts = { bookType: "xlsx", bookSST: false, type: "binary" };
let row = 1;
sheet.getRow(row).values = ["操作用户:", userinfo];
row += 1;
let before = this.excel_statusList[this.implementLeftData]
sheet.getRow(row).values = ["操作前状态:", before];
if (this.implementLeftData) {
sheet.getRow(row).getCell(2).fill = {
type: "pattern",
pattern: "solid",
fgColor: {
argb: `${
this.Excel_Status_cclor[
this.implementLeftData
]
}`,
},
};
}
row += 1;
let after = this.excel_statusList[this.implementRightData]
sheet.getRow(row).values = ["操作后状态:", after];
if (this.implementRightData) {
sheet.getRow(row).getCell(2).fill = {
type: "pattern",
pattern: "solid",
fgColor: {
argb: `${
this.Excel_Status_cclor[
this.implementRightData
]
}`,
},
};
}
row += 1;
sheet.getRow(row).values = ["操作时间:", new Date()];
row += 1;
sheet.getRow(row).values = [
"操作前检查内容",
"参考值",
"识别结果",
"操作后检查内容",
"参考值",
"识别结果",
];
for (let i = 0; i < data.length; i++) {
row += 1;
sheet.getRow(row).values = data[i];
}
// for (let i = 0; i < data[2].length; i++) {}
// 给单个单元格设置样式
// sheet.getCell("A1").fill = {
// type: "pattern",
// pattern: "solid",
// fgColor: { argb: "FFFF0000" },
// };
// 插入图片
// this.$axios
// .get("/static/img/bg1.png", { responseType: "arraybuffer" })
// .then((res) => {
// console.log(res);
// let buffer = res.data;
// const imageId2 = workbook.addImage({
// buffer: buffer,
// extension: "png",
// });
// sheet.addImage(imageId2, {
// tl: { col: 0, row: 3 },
// ext: { width: 50, height: 10 },
// editAs: "absolute",
// });
// })
// .catch((err) => {
// console.log(err);
// });
workbook.xlsx
.writeBuffer(wopts)
.then((res) => {
window.test = res;
common.saveAs(res, "test.xlsx", {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
})
.catch((err) => {
this.$Tips({ content: "生成文件失败", tyep: "error" });
});
// 通过 buffer 将图像添加到工作簿
// const row = sheet.getRow(5);
},
exceljs代码
最新推荐文章于 2026-06-04 13:43:07 发布
本文介绍了如何在前端项目中使用exceljs库进行Excel文件的读取、写入和操作,详细阐述了关键API的使用方法及实战示例,帮助开发者高效地处理Excel数据。
4929

被折叠的 条评论
为什么被折叠?



