使用场景:
无api提供excel,前端根据数据,导出excel文档(自定义excel内容样式)。
使用技术:excelJs插件
实现图例:
实现代码:
//创建工作表
const workbook = new Workbook();
workbook.creator = "sheet1";
workbook.lastModifiedBy = "sheet1";
workbook.created = new Date();
workbook.modified = new Date();
// 将工作簿日期设置为 1904 年日期系统
workbook.properties.date1904 = true;
const worksheet = workbook.addWorksheet("sheet1");
worksheet.properties.defaultColWidth = 12; // 默认列宽
// worksheet.properties.defaultRowHeight = 20; // 默认行高
//定义表格标题
const Targetrow = worksheet.getRow(1);
Targetrow.height = 20;
const Targetcell = worksheet.getCell(`A1`);
worksheet.mergeCells(`A1:${this.conversionASCII('A', colCount - 1, '+')}1`);
Targetcell.value = `${this.translateService.instant('dj-default-目标机型') + ':' + data.viewDetailTargetNo}`;
Targetcell.font = { name: "宋体", family: 4, size: 11, bold: true }; // 字体
Targetcell.alignment = { vertical: "middle", horizontal: "left" }; //对齐
// 定义表格内容
worksheet.addTable({
name: "TargetTable",
ref: `A2`,
displayName: "目标机型",
headerRow: true, // 在表格顶部显示标题
totalsRow: false, // 在表格底部显示总计
columns: this.formatCol(data.openStageFlag, data.compareMethodFlag),
rows: targetModelExcels,
});
//第二个表格定义同上,代码省略
//下载excel文档
let myDate = new Date().toLocaleString();
let time = myDate.split(/(?:\/|:|\s)/).join('');
//下载excel表格
workbook.xlsx.writeBuffer().then((buffer) => {
// eslint-disable-next-line no-undef
saveAs(
new Blob([buffer], {
type: "application/octet-stream",
}),
'BOM_' + time + '.xlsx'
);
});