首页 > 解决方案 > 使用带有 node.js 的模块 exceljs 从特定行/列开始写入数据

问题描述

我想从特定的行/列开始将数据写入 xlsx 文件。我找到了很多关于 exceljs 的网站,但没有一个对我有帮助。

for (let i = 0; i < people.length; i++) {
    let {name, age} = people[i]

    worksheet.addRow({name: name, age: age})
}

worksheet.eachRow(function(row, rowNumber) {
    row.eachCell(function(cell, colNumber) {
        // some code
    }
}

标签: javascriptnode.jsxlsxexceljs

解决方案


您可以使用 Excel JS API,代码示例显示了如何将数据写入特殊的行/列。要点可以在以下位置找到:https ://gist.github.com/lumine2008/796915d73a56a9e89ff9393c77845759

 await Excel.run(async (context) => {
    context.workbook.worksheets.getItemOrNullObject("Sample").delete();
    const sheet = context.workbook.worksheets.add("Sample");

    const data = [
      ["Product", "Qty", "Unit Price", "Total Price"],
      ["Almonds", 2, 7.5, "=C3 * D3"],
      ["Coffee", 1, 34.5, "=C4 * D4"],
      ["Chocolate", 5, 9.56, "=C5 * D5"]
    ];

    const range = sheet.getRange("B2:E5");
    range.values = data;
    range.format.autofitColumns();

    const header = range.getRow(0);
    header.format.fill.color = "#4472C4";
    header.format.font.color = "white";

    sheet.activate();

    await context.sync();

推荐阅读