我如何从 nodejs 将一些数据插入 excel 的单元格

How can i insert some data to a cell of excel from nodejs

我想使用 nodejs 将数据插入 excel 中的单元格。有人可以帮我做同样的 nodejs 吗?我必须从我的 nodejs 代码中读取一个 excel 文件,并将数据插入到我的 excel sheet 的特定单元格中。 我尝试了一个代码,其中下面的代码给出了如下所示的输出,我怎样才能在 v ie 中访问该数据,john 代码片段

console.log(JSON.stringify(worksheet['A2']))

输出

{"t":"s","v":"john","r":"<t>jon</t>","h":"john","w":"jon"}

fullcode - 我尝试将数据插入 excel

的单元格
const XLS

    X = require('xlsx');
    let workbook = XLSX.readFile('test1.xlsx');
    let sheetName = workbook.SheetNames[0];
    console.log(sheetName+'    sheetName')
    let worksheet = workbook.Sheets[sheetName];
    console.log(worksheet['A2']+'    worksheet a2')
    console.log(JSON.stringify(worksheet['A2']))
       worksheet['A2'].value = 'test';
      var cell = worksheet.getCell('A2');
      //cell.value = 'test';
      sheetName.getCell("A2").value = "test"

如果您熟悉 Pandas DataFrame 的结构(基本上是一个键值对数组,键是列名),那么 convert-excel-to-json is probably a good bet for what you're looking for. It's fairly straightforward to set-up, and should let you traverse the converted sheet and edit it, as a JSON object. Post this, you can go back to the excel file-format, using something like json2xls.

const result = excelToJson({
    sourceFile: 'SOME-EXCEL-FILE.xlsx'
});

result.sheet1[0]["Col_Name"] = "Foo"

然而,这对于非常大的 excel sheet 来说可能不可行,特别是如果不方便将全部内容保存在内存中。如果是这种情况,那么逐行复制 sheet 数据并修改所需的行可能是可行的方法。我将在这里结束这个答案,因为我不确定这是否是您正在寻找的,但如果是,我很乐意提供帮助:)