NodeJS 操作 Excel 文件

使用 node-xlsx-writestream 可以操作 excel 文件。

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var XLSXWriter = require('xlsx-writestream');
var fs = require('fs');
var fileName = "test.xlsx"
var writer = new XLSXWriter(fileName, {} /* options */);
var wirteStream = fs.createWriteStream(fileName)
// After instantiation, you can grab the readstream at any time.
writer.getReadStream().pipe(wirteStream);
// Optional: Adjust column widths
writer.defineColumns([
{width: 30}, // width is in 'characters'
{width: 10}
]);
// Add some rows
writer.addRow({
"Name": "Bob",
"Location": "Sweden"
});
writer.addRow({
"Name": "Alice",
"Location": "France"
});
// Add a row with a hyperlink
writer.addRow({
"Name": {value: "Bill", hyperlink: "http://www.thegatesnotes.com"},
"Location": "Seattle, Washington"
});
wirteStream.on('finish', function () {
// finish
});
// Finalize the spreadsheet. If you don't do this, the readstream will not end.
writer.finalize();

Link