NodeJS:将智能JSON转换为Excel文件 [英] NodeJS : smart JSON conversion to Excel file
问题描述
我正在NodeJS工作,我想将一个JSON格式的对象导出到Excel文件。
I'm working in NodeJS and I would like to export a JSON-format object to an Excel file.
我很清楚,有(至少)为此目的的三个npm包,但到目前为止,这些都没有给我我正在做的输出。
I am well aware that there are (at least) three npm packages for that purpose, but so far none of these gave me the output I'm dreaming of.
这是我拥有的javascript对象:
Here is the javascript object I have :
var myObject =
{
hashkey1 : {
keyA : dataA1,
keyB : dataB2
}
hashkey2 : {
keyA : dataA2,
keyB : dataB2
}
};
(或.xlsx)(或任何电子表格格式)我的梦想有一行为每个散列键。在每行中:第一列将是hashkeyX,第二列将是dataAX,第三列将是dataBX。
The .xls (or .xlsx)(or any spreadsheet format) of my dreams has one line for each hashkey. On each line : first column would be the hashkeyX, second column would be the dataAX, third column would be the dataBX.
是否可以使用可用的工具,还是从头开始编码?任何建议,以获得这个结果附近的任何地方?
Is it possible to achieve such a result using available tools, or do I have to code it from scratch ? Any advice to get anywhere near this result ?
推荐答案
您可以写入csv(逗号分隔值)文本文件,图书馆。默认情况下,此扩展在Excel中打开。
You can write to csv (comma-separated values) text file without any additional library. This extension open in Excel by default.
var fs = require('fs');
var file = fs.createWriteStream('file.csv', {'flags': 'w', autoClose: true});
var result = '';
for (var hashkey in myObject)
result += hashkey + ';' + myObject[hashkey].keyA + ';' + myObject[hashkey].keyB + '\n';
file.write(result);
这篇关于NodeJS:将智能JSON转换为Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!