json转excel

前面有介绍过excel文件转换成json文件的方法,那json文件转excel文件呢?如果json文件里数据格式都是统一的话,那么也比较容易就转。

我们假设json文件中是一个json数组,每条json数据的属性字段都一样,手写一段node.js例程如下:

javascript 复制代码
//json2excel.js
const xlsx = require("node-xlsx");
const fs = require("fs");

let jsonfile=process.argv.slice(2)[0]
let excelfile=process.argv.slice(2)[1]
console.log("json file is "+jsonfile+" , and excel file is "+excelfile)

var jlist=require(".\\"+jsonfile);
//console.log(jlist);

var xlist=[{
name:"sheet1",
data:[Object.keys(jlist[0])]
}]

jlist.forEach(item=>{xlist[0].data.push(Object.values(item))});
//console.log(xlist);
var excelbuf=xlsx.build(xlist);
fs.writeFileSync(excelfile,excelbuf)
console.log("done!");

在项目文件夹下有待转换json文件,转出的xlsx文件也存放在此,这样就可以执行以下命令转换json文件到excel文件了

bash 复制代码
node json2excel.js source.json target.xlsx
相关推荐
温轻舟2 小时前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
q***09802 小时前
Windows 11 如何配置node.js
windows·node.js
pixle03 小时前
从零学习Node.js框架Koa 【六】Koa文件上传下载实现:@koa/multer 与 koa-send 深度解析
node.js·web·koa·js·全栈·服务端·文件上传下载
TDengine (老段)3 小时前
TDengine 转换函数 TO_JSON 用户手册
android·大数据·数据库·json·时序数据库·tdengine·涛思数据
q***47183 小时前
Windows 上彻底卸载 Node.js
windows·node.js
WarPigs5 小时前
Unity编辑器开发笔记
unity·编辑器·excel
wow_DG7 小时前
【Python✨】VS Code 秒开 Python 类型检查:一招 mypy + settings.json 让你的 Bug 原地现形!
python·json·bug
allbs7 小时前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel
洞窝技术7 小时前
一键屏蔽某国IP访问实战
前端·nginx·node.js
weixin79893765432...7 小时前
使用 node.js 的心得
node.js