Skip to content

Instantly share code, notes, and snippets.

@Spike-Leung
Last active March 10, 2023 07:09
Show Gist options
  • Save Spike-Leung/fb936ed230018c35cf5202a3d6bfc1da to your computer and use it in GitHub Desktop.
Save Spike-Leung/fb936ed230018c35cf5202a3d6bfc1da to your computer and use it in GitHub Desktop.
Convert json to xlsx
/**
* 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