我是一个 nodejs 程序员。现在我有一个要以 Excel 文件格式保存的数据表。我该怎么做呢?
我找到了一些 Node 库。但其中大多数是 Excel 解析器而不是 Excel 编写器。我使用的是 Linux 服务器。因此需要可以在 Linux 上运行的东西。如果您知道任何有用的库,请告诉我。
或者有没有办法可以将 CSV 文件转换为 xls 文件(以编程方式)?
最佳答案
excel4node 是一个维护的原生 Excel 文件创建器根据官方规范构建。它与另一个答案中提到的 msexcel-builder 类似,但维护得更好。
// Require library
var excel = require('excel4node');
// Create a new instance of a Workbook class
var workbook = new excel.Workbook();
// Add Worksheets to the workbook
var worksheet = workbook.addWorksheet('Sheet 1');
var worksheet2 = workbook.addWorksheet('Sheet 2');
// Create a reusable style
var style = workbook.createStyle({
font: {
color: '#FF0800',
size: 12
},
numberFormat: '$#,##0.00; ($#,##0.00); -'
});
// Set value of cell A1 to 100 as a number type styled with paramaters of style
worksheet.cell(1,1).number(100).style(style);
// Set value of cell B1 to 300 as a number type styled with paramaters of style
worksheet.cell(1,2).number(200).style(style);
// Set value of cell C1 to a formula styled with paramaters of style
worksheet.cell(1,3).formula('A1 + B1').style(style);
// Set value of cell A2 to 'string' styled with paramaters of style
worksheet.cell(2,1).string('string').style(style);
// Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
worksheet.cell(3,1).bool(true).style(style).style({font: {size: 14}});
workbook.write('Excel.xlsx');
关于excel - 如何使用 Nodejs 创建 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450412/