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 小时前
如何在WPS打开的word、excel文件中,使用AI?
人工智能·word·excel·wps
垣宇3 小时前
Vite 和 Webpack 的区别和选择
前端·webpack·node.js
爱吃南瓜的北瓜3 小时前
npm install 卡在“sill idealTree buildDeps“
前端·npm·node.js
翻滚吧键盘3 小时前
npm使用了代理,但是代理软件已经关闭导致创建失败
前端·npm·node.js
@LitterFisher4 小时前
Excell 代码处理
前端·javascript·excel
winfredzhang4 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
浪九天4 小时前
node.js的版本管理
node.js
oh,huoyuyan6 小时前
火语言RPA--Excel插入空列
excel·rpa
浪九天6 小时前
node.js的常用指令
node.js
浪九天8 小时前
Vue 不同大版本与 Node.js 版本匹配的详细参数
前端·vue.js·node.js