個人博客:柚子青年。
原文鏈接:xlsx - json 互轉
xlsx 轉 json
const fs = require('fs'); const xlsx2json = require('xlsx2json'); xlsx2json( './文檔.xlsx', // url { dataStartingRow: 3, // 第幾行開始 mapping: { // 解析 key value 'key1': 'A', 'key2': 'B', } } ).then(jsonArray => { // 輸出數組 格式自行log fs.writeFileSync('./文檔.json', JSON.stringify(jsonArray)); });
json 轉 xlsx
const fs = require('fs'); const json2xls = require('json2xls'); const json = require("./json"); let jsonArr = []; for (let jsonKey in json) { jsonArr.push({ "A": json[jsonKey].cn, // A 內容 "B": json[jsonKey].en, // B 內容 }); } fs.writeFileSync('./data.xlsx', json2xls(jsonArr), 'binary');