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
相关推荐
Q_Q196328847543 分钟前
python+django/flask的医院财务管理系统
spring boot·python·django·flask·node.js
cmdyu_1 小时前
修复mac系统下node22安装模块报fatal error: ‘memory‘ file not found错误的问题
macos·npm·node.js
nvvas2 小时前
npm : 无法加载文件 D:\nvm\nodejs\npm.ps1,因为在此系统上禁止运行脚本问题解决
前端·npm·node.js
企鹅侠客2 小时前
用AI写了一个Excel 批量插图工具
excel·excel批量插图
code bean3 小时前
【C#笔记】Newtonsoft.Json 中 `[JsonIgnore]` 的作用详解
笔记·c#·json
友莘居士4 小时前
Ganache-CLI以太坊私网JSON-RPC接口执行环境搭建
网络协议·rpc·json·环境搭建·以太坊
阿珊和她的猫4 小时前
Webpack Loader 和 Plugin 实现原理详解
前端·webpack·node.js
m5655bj4 小时前
Python 查找并高亮显示指定 Excel 数据
开发语言·python·excel
luckyzlb6 小时前
03-node.js & webpack
前端·webpack·node.js
缺点内向6 小时前
Java:高效删除Excel中的空白行和列
java·开发语言·excel