Last active
March 10, 2023 07:09
-
-
Save Spike-Leung/fb936ed230018c35cf5202a3d6bfc1da to your computer and use it in GitHub Desktop.
Convert json to xlsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* input.json looks like: | |
* | |
* { "name": "name" } | |
*/ | |
// @deno-types="https://cdn.sheetjs.com/xlsx-0.19.2/package/types/index.d.ts" | |
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.19.2/package/xlsx.mjs'; | |
import { readFileSync } from "https://deno.land/[email protected]/node/fs.ts"; | |
// 读取 JSON 文件 | |
const data = readFileSync('./input.json', 'utf-8') | |
const jsonData = JSON.parse(data); | |
const exportData = Object.entries(jsonData).map(([key, value]) => { | |
return { | |
key, | |
chinese: value | |
} | |
}) | |
// 转换为工作簿 | |
const worksheet = XLSX.utils.json_to_sheet(exportData); | |
const workbook = XLSX.utils.book_new(); | |
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); | |
// 将工作簿写入 Excel 文件 | |
XLSX.writeFile(workbook, 'output.xlsx'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment