本文共 1243 字,大约阅读时间需要 4 分钟。
ExcelJS 是一个 Node.js 模块,可用来读写和操作 XLSX 和 JSON 电子表格数据和样式。
示例代码:
// Add column headers and define column keys and widths// Note: these column structures are a workbook-building convenience only,// apart from the column width, they will not be fully persisted.worksheet.columns = [ { header: "Id", key: "id", width: 10 }, { header: "Name", key: "name", width: 32 }, { header: "D.O.B.", key: "DOB", width: 10 }];// Access an individual columns by key, letter and 1-based column numbervar idCol = worksheet.getColumn("id");var nameCol = worksheet.getColumn("B");var dobCol = worksheet.getColumn(3);// set column properties// Note: will overwrite cell value C1dobCol.header = "Date of Birth";// Note: this will overwrite cell values C1:C2dobCol.header = ["Date of Birth", "A.K.A. D.O.B."];// from this point on, this column will be indexed by "dob" and not "DOB"dobCol.key = "dob";dobCol.width = 15;// iterate over all current cells in this columndobCol.eachCell(function(cell, rowNumber) { // ...});// iterate over all current cells in this column including empty cellsdobCol.eachCell({ includeEmpty: true }, function(cell, rowNumber) { // ...});
文章转载自 开源中国社区 [