第一部分 下载
npm install exceljs
第二部分 基本的配置
var Excel = require("exceljs");
var workbook = new Excel.Workbook();
// 基本的创建信息
workbook.creator = "Me";
workbook.lastModifiedBy = "Her";
workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
workbook.lastPrinted = new Date(2016, 9, 27);
// 视图大小, 打开Excel时,整个框的位置,大小
workbook.views = [
{
x: 0,
y: 0,
1000,
height: 2000,
firstSheet: 0,
activeTab: 1,
visibility: "visible"
}
];
// 标签创建
var worksheet = workbook.addWorksheet("第一个标签");
// 带颜色的
var worksheet2 = workbook.addWorksheet("第二个标签", { properties: { tabColor: { argb: "FFC0000" } } });
// 遍历标签
workbook.eachSheet((worksheet, sheetId) => {
console.log("标签ID:", sheetId)
})
// console.log(worksheet);
// 删除一个标签
workbook.removeWorksheet(2)
var firstSheet = workbook.getWorksheet(1);
console.log("标签信息-id", firstSheet.id);
console.log("获取总的:行/实际行 /列/实际列 个数: ", firstSheet.rowCount, firstSheet.actualColumnCount, firstSheet.columnCount, firstSheet.actualColumnCount);
// 添加那个筛选箭头
worksheet.autoFilter = 'A1:C1';
worksheet.getRow(5).font = { size: 14, bold: true };
worksheet.getCell("A2").value = "Site";
worksheet.getCell("A2").font = {
name: "Arial Black",
color: { argb: "FF00FF00" },
family: 2,
size: 14,
italic: true,
bold: true
};
// save workbook to disk
workbook.xlsx.writeFile("first.xlsx").then(function() {
console.log("saved");
});
第三部分 行列的操作
看代码看注释
var Excel = require("exceljs");
var workbook = new Excel.Workbook();
// 标签创建
var worksheet = workbook.addWorksheet("第一个标签");
// 带颜色的
var worksheet2 = workbook.addWorksheet("第二个标签", {
properties: {
tabColor: {
argb: "FFC0000"
}
}
});
// 设置列
worksheet.columns = [{
header: 'Rating Period',
key: 'id',
38
},
{
header: 'Name',
key: 'name',
32,
},
{
header: 'D.O.B.',
key: 'DOB',
10,
style: {
numFmt: 'dd/mm/yyyy'
}
}
];
/// 根据ID添加值
worksheet.addRow({
id: 1,
name: 'John Doe',
dob: new Date(1970, 1, 1)
});
worksheet.addRow({
id: 2,
name: 'Jane Doe',
dob: new Date(1965, 1, 7)
});
worksheet.getCell(1).value = "Z";
// 直接赋值
worksheet.getCell('A6').value = "1989";
// 合并单元格
worksheet.mergeCells('A4:A7');
// 合并四个格子
worksheet.mergeCells('A10', 'B11');
// ===== 格式化显示,
// 数字 1.6 显示 '1 3/5'
worksheet.getCell('A1').value = 1.6;
worksheet.getCell('A1').numFmt = '# ?/?';
// 显示 1.60%
worksheet.getCell('B1').value = 0.016;
worksheet.getCell('B1').numFmt = '0.00%';
// ===== 字体显示
worksheet.getCell('A3').font = {
// 字体名
name: 'Comic Sans MS',
// Font family for fallback. An integer value.
family: 4,
// 字体大小
size: 16,
// 下划线
underline: true,
// 加粗
bold: true,
};
worksheet.getCell('A3').value = "测试字体"
// ==== 对齐方式
worksheet.getCell('A1').alignment = {
vertical: 'top',
horizontal: 'left'
};
worksheet.getCell('B1').alignment = {
vertical: 'middle',
horizontal: 'center'
};
worksheet.getCell('C1').alignment = {
vertical: 'bottom',
horizontal: 'right'
};
// ===== 边框
worksheet.getCell('A1').border = {
top: {
style: 'double',
color: {
argb: 'FF00FF00'
}
},
left: {
style: 'double'
},
bottom: {
style: 'thin'
},
right: {
style: 'thin'
}
};
// ==== 填充颜色
worksheet.getCell('A1').fill = {
// 模式
type: 'pattern',
// 填充
pattern: 'solid',
// fgColor: {
// argb: 'FFFF0000'
// },
// 背景色
bgColor: {
argb: 'FF0000FF'
}
};