excel - 如何使用 Nodejs 创建 Excel 文件?

标签 excel node.js file-io export-to-excel npm

我是一个 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/

相关文章:

file-io - 为什么命名管道的只读打开会阻塞?

vba - 关闭工作簿时的奇怪行为

java - Apache POI将图像插入到excel文件中,但不显示

javascript - 在 Mocha 测试中使用 for 循环

php - ApiDoc错误: parser plugin "apimethod" not found

excel - 如何重新启动文件输入循环

java - 在 Android 中逐字读取文件的最佳方法是什么

excel - 将 Visual Basic 编辑器的语言更改为英语

excel - 具有来自另一个工作簿的 5 个条件的 Sumproduct 运行时间太长

node.js - 如何在 Node.js 项目中包含 css 和 js 文件