如何使用 Nodejs 创建 Excel 文件? [英] How to create an Excel File with Nodejs?

查看:32
本文介绍了如何使用 Nodejs 创建 Excel 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 nodejs 程序员.现在我有一个数据表,我想以 Excel 文件格式保存.我该怎么做?

I am a nodejs programmer . Now I have a table of data that I want to save in Excel File format . How do I go about doing this ?

我发现了一些 Node 库.但他们中的大多数是 Excel Parsers 而不是 Excel Writers.我使用的是 Linux 服务器.因此需要一些可以在 Linux 上运行的东西.如果您知道有任何有用的库,请告诉我.

I found a few Node libraries . But most of them are Excel Parsers rather than Excel Writers .I am using a Linux Server . Hence need something that can run on Linux . Please let me know if there are any helpful libraries that you know of .

或者有什么方法可以将 CSV 文件转换为 xls 文件(以编程方式)?

Or is there a way I can convert a CSV file to an xls file ( programmatically ) ?

推荐答案

excel4node 是一个维护, 本机 Excel 文件创建器根据官方规范构建.它与另一个答案中提到的 mxexcel-builder 类似,但维护得更好.

excel4node is a maintained, native Excel file creator built from the official specification. It's similar to, but more maintained than mxexcel-builder mentioned in the other answer.

// 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');

这篇关于如何使用 Nodejs 创建 Excel 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆