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 小时前
Node.js 编程实战:理解 Buffer 与 Stream
后端·node.js·trae
程序员爱钓鱼3 小时前
Node.js 编程实战:npm和yarn基础使用
后端·node.js·trae
程序员爱钓鱼3 小时前
Node.js 编程实战:CommonJS 与ES6 模块
后端·node.js·trae
heartbeat..4 小时前
使用 Apache POI 实现 Excel 文件读写(导入 导出)操作的工具类
java·apache·excel·文件
星空椰7 小时前
Java Excel转PDF
pdf·excel
孟祥_成都8 小时前
nest.js / hono.js 一起学!hono的设计思想!
前端·node.js
星空椰8 小时前
Windows 使用nvm多版本管理node.js
windows·node.js
我叫唧唧波8 小时前
【自动化部署】基于Docker构建CI/CD流水线
ci/cd·docker·node.js
别叫我->学废了->lol在线等8 小时前
自然语言转成formily+shadcn组件的jsonschema
javascript·json
ohoy9 小时前
easypoi 带图片导入 公司表
excel