我正在处理NodeJS项目,我需要修改现有的Excel电子表格,但无需更改原始格式或链接,只需将值添加到几个单元格即可。目前,我一直使用exceljs
来修改电子表格,效果很好,但是输出会删除所有原始样式,格式和链接。
是否有另一个javascript npm或库可以帮助解决此问题,或者exceljs
是否可以解决?
这是我到目前为止所拥有的:
Fetch('url of the file')
.then(res => res.buffer())
.then(buffer => {
var workbook = new Excel.Workbook();
workbook.xlsx.load(buffer).then(function() {
var worksheet = workbook.getWorksheet(1);
var row = worksheet.getRow(4);
row.getCell(2).value = 'It works!';
row.commit();
workbook.xlsx.write(stream)
.then(function() {
// done
// here I take the stream and upload it to an AWS S3 bucket
});
});
})
谢谢!
答案 0 :(得分:1)
对于那些正在寻找TypeScript版本的人...
import { fromFileAsync } from 'xlsx-populate';
...
fromFileAsync('input.xlsx').then(workbook => {
let ws=workbook.sheet("Sheet1");
ws.cell("C1").value("value in C1");
ws.cell("C2").value("value in C2");
return workbook.toFileAsync('input.xlsx');
});
希望对某人有用。 ?
答案 1 :(得分:0)
我发现了另一个npm
,它很棒!是xlsx-populate。
以下是与exceljs
用来完成相同工作的代码:
Fetch('file url')
.then(res => res.buffer())
.then(buffer => {
XlsxPopulate.fromDataAsync(buffer)
.then(workbook => {
// Make edits.
workbook.sheet(0).cell("A1").value("foo");
// Get the output
return workbook.outputAsync();
})
.then(data => {
// upload data to AWS S3
})
.catch(err => console.error(err));
});
希望这对经历相同情况的人有所帮助:)