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
相关推荐
李游Leo9 小时前
Node.js 多版本管理与 nvm/nvs 使用全流程(含国内镜像加速与常见坑)
node.js
Q_Q196328847510 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
陈随易16 小时前
适合中国宝宝的AI编程神器,文心快码
前端·后端·node.js
Q_Q196328847518 小时前
python+springboot大学生心理测评与分析系统 心理问卷测试 自动评分分析 可视化反馈系统
开发语言·spring boot·python·django·flask·node.js·php
Eiceblue20 小时前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
EndingCoder20 小时前
Electron 新特性:2025 版本更新解读
前端·javascript·缓存·electron·前端框架·node.js·桌面端
小孔龙21 小时前
01.Kotlin Serialization - 基础用法
kotlin·json
machinecat21 小时前
node,小程序合成音频的方式
前端·node.js
acaad1 天前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel
周杰伦fans1 天前
.NET 轻量级处理 Excel 文件库 - MiniExce
windows·.net·excel