1.有时候我们开发的时候会需要将json文件转化成excel文件,这种我们应该怎么操作呢,直接上代码
首先我们需要安装一个处理excel表格的包
npm i node-xlsx
接下来对数据进行处理
const fs = require('fs')
const xlsx = require('node-xlsx')
const zhExcelDate = [['键','值']]
const enExcelDate = [['键','值']]
const zhLangStr = fs.readFileSync('./1.json','utf-8')
const zhLangObj = JSON.parse(zhLangStr)
const enLangStr = fs.readFileSync('./2.json','utf-8')
const enLangObj = JSON.parse(enLangStr);
Object.keys(zhLangObj).forEach(key => {
zhExcelDate.push([key,zhLangObj[key]])
})
Object.keys(enLangObj).forEach(key => {
enExcelDate.push([key,enLangObj[key]])
})
const sheetOptions = {'!cols':[{wch:30},{wch:30}]}
const bufferZh = xlsx.build([{name:'中文词条',data:zhExcelDate}],{
sheetOptions
})
const bufferEn = xlsx.build([{name:'英文词条',data:enExcelDate}],{
sheetOptions
})
fs.writeFileSync('./en.xlsx',bufferEn,{flag:'w'})
fs.writeFileSync('./zh.xlsx',bufferZh,{flag:'w'})
这样就可以把json文件转化为excel文件了