{"id":20322699,"url":"https://github.com/asasugar/custom-json2excel","last_synced_at":"2025-04-07T10:19:37.345Z","repository":{"id":42083502,"uuid":"144811844","full_name":"asasugar/custom-json2excel","owner":"asasugar","description":"📃 传入json生成excel，可自定义表格头部名称和过滤列","archived":false,"fork":false,"pushed_at":"2025-01-15T10:25:07.000Z","size":519,"stargazers_count":44,"open_issues_count":1,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T09:05:01.511Z","etag":null,"topics":["excel","javascript","json","json2excel","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asasugar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-15T05:58:25.000Z","updated_at":"2025-01-15T10:25:09.000Z","dependencies_parsed_at":"2024-06-20T23:24:23.927Z","dependency_job_id":"c7fbbe76-a911-430a-b748-8ca35876307e","html_url":"https://github.com/asasugar/custom-json2excel","commit_stats":{"total_commits":50,"total_committers":7,"mean_commits":7.142857142857143,"dds":0.6799999999999999,"last_synced_commit":"3fb9c8d50adf0c30d8816a5dfca67ebec677102e"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asasugar%2Fcustom-json2excel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asasugar%2Fcustom-json2excel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asasugar%2Fcustom-json2excel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asasugar%2Fcustom-json2excel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asasugar","download_url":"https://codeload.github.com/asasugar/custom-json2excel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247631834,"owners_count":20970069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["excel","javascript","json","json2excel","typescript"],"created_at":"2024-11-14T19:24:20.794Z","updated_at":"2025-04-07T10:19:37.309Z","avatar_url":"https://github.com/asasugar.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @asasugar-use/custom-json2excel\n\n## npm 账号切换新账号了，重新发包，包名更新@asasugar-use/custom-json2excel\n\n## 更新内容\n\n### 3.1.9\n\n- 默认导出 `Json2excel` 类调整为具名导出 `JsonToExcel` 类和 `excelToJson` 方法\n- 更新 `example` ，react 示例和 html 示例\n\n### 3.1.8\n\n- 新增 `sheetToJson` 方法，支持导入 excel 输出json功能\n- 使用 `rslib` 重构打包，增加注释\n- 更新 `example` ，react 示例和 html 示例\n\n### 3.1.7\n\n- 更新包名@asasugar-use/custom-json2excel 和文档\n\n### v3.1.6\n\n- 修复 0 开头的字符转化为 excel 被过滤的问题[#19](https://github.com/asasugar/custom-json2excel/issues/19)\n\n### v3.1.5\n\n- 修复打包后的兼容问题\n- 修复 keyMap 问题\n\n### v3.1.4\n\n- 新增 `scope` 参数\n- 新增 `onError` 回调函数\n- bug 修复\n\n### v3.1.3\n\n- 重构 ts 代码，分别导出 cjs, esm, umd 模块包\n\n### v3.1.2\n\n- [修复导出数据重复为最后一项的 bug](https://github.com/asasugar/custom-json2excel/issues/14)\n\n### v3.1.1\n\n- 修复 ie 下载问题\n\n- 修复未指定 kepMap 导致的 filters 失效问题\n\n- 增加了`orderedKey`字段，可指定列顺序\n\n## Plugin setup\n\n```\nyarn add @asasugar-use/custom-json2excel\nor\nnpm install @asasugar-use/custom-json2excel\nor\npnpm add @asasugar-use/custom-json2excel\n```\n\n## How to use?\n\n### 方法一\n\n\u003e (示例: example/react/src/router/antd-layout.tsx)\n\n1、直接转化 json：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst json2excel = new JsonToExcel({ data });\njson2excel.generate();\n```\n\n![20190520174344.png](https://i.loli.net/2019/05/20/5ce276d35737f64374.png)\n\n2、自定义头部无需过滤字段时的使用方式：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst keyMap = {\n  name: '姓名',\n  age: '年龄',\n  sex: '性别',\n  companyName: '公司名称',\n  companyAddress: '公司地址',\n};\nconst json2excel = new JsonToExcel({ data, keyMap });\njson2excel.generate();\n```\n\n![20190520174449.png](https://i.loli.net/2019/05/20/5ce27712b3c4880090.png)\n\n3、需要按照字段顺序返回表格列时的使用方式：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst keyMap = {\n  name: '姓名',\n  age: '年龄',\n  sex: '性别',\n  companyName: '公司名称',\n  companyAddress: '公司地址',\n};\nconst orderedKey = ['sex', 'companyName', 'name'];\nconst json2excel = new JsonToExcel({ data, keyMap, orderedKey });\njson2excel.generate();\n\n// data会转化成=\u003e\n[\n  {\n    性别: '男',\n    公司名称: '公司1',\n    姓名: '哈哈',\n  },\n  {\n    性别: '女',\n    公司名称: '公司2',\n    姓名: '呵呵',\n  },\n  {\n    性别: '男',\n    公司名称: '公司3',\n    姓名: '嘻嘻',\n  },\n  {\n    性别: '女',\n    公司名称: '公司4',\n    姓名: '啦啦',\n  },\n];\n```\n\n4、需要过滤字段时的使用方式：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst keyMap = {\n  name: '姓名',\n  age: '年龄',\n  sex: '性别',\n  companyName: '公司名称',\n  companyAddress: '公司地址',\n};\nconst filters = ['sex'];\nconst json2excel = new JsonToExcel({ data, keyMap, filters });\njson2excel.generate();\n```\n\n![20190520174515.png](https://i.loli.net/2019/05/20/5ce2772bf1a3718069.png)\n\n5、需要表格标题时的使用方式：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst keyMap = {\n  name: '姓名',\n  age: '年龄',\n  sex: '性别',\n  companyName: '公司名称',\n  companyAddress: '公司地址',\n};\nconst filters = ['sex'];\nconst title = [\n  { name: '个人信息', colspan: 3 },\n  { name: '公司信息', colspan: 2 },\n];\nconst json2excel = new JsonToExcel({ data, keyMap, filters, title });\njson2excel.generate();\n```\n\n![20190520174536.png](https://i.loli.net/2019/05/20/5ce27741a660624320.png)\n\n6、绑定回调函数的使用方式：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n  },\n];\nconst keyMap = {\n  name: '姓名',\n  age: '年龄',\n  sex: '性别',\n  companyName: '公司名称',\n  companyAddress: '公司地址',\n};\nconst filters = ['sex'];\nconst title = [\n  { name: '个人信息', colspan: 3 },\n  { name: '公司信息', colspan: 2 },\n];\nconst json2excel = new JsonToExcel({\n  data,\n  keyMap,\n  filters,\n  title,\n  onStart: () =\u003e {\n    console.log('开始');\n  },\n  onSuccess: () =\u003e {\n    console.log('成功');\n  },\n  onError: (err) =\u003e {\n    console.log(err);\n  },\n});\njson2excel.generate();\n```\n\n7、scope 使用：\n\n```js\nimport { JsonToExcel } from '@asasugar-use/custom-json2excel';\nconst data = [\n  {\n    name: '哈哈',\n    age: 1,\n    sex: '男',\n    companyName: '公司1',\n    companyAddress: '公司地址1',\n    love: {\n      study: {\n        book: '语文',\n      },\n    },\n    v: {\n      key: 1,\n    },\n  },\n  {\n    name: '呵呵',\n    age: 2,\n    sex: '女',\n    companyName: '公司2',\n    companyAddress: '公司地址2',\n    love: {\n      study: {\n        book: '数学',\n      },\n    },\n    v: {\n      key: 2,\n    },\n  },\n  {\n    name: '嘻嘻',\n    age: 3,\n    sex: '男',\n    companyName: '公司3',\n    companyAddress: '公司地址3',\n    love: {\n      study: {\n        book: '英语',\n      },\n    },\n    v: {\n      key: 3,\n    },\n  },\n  {\n    name: '啦啦',\n    age: 4,\n    sex: '女',\n    companyName: '公司4',\n    companyAddress: '公司地址4',\n    love: {\n      study: {\n        book: '化学',\n      },\n    },\n    v: {\n      key: 4,\n    },\n  },\n];\n\nconst scope = {\n  love: { study: 'book' },\n  v: 'key',\n};\nconst json2excel = new JsonToExcel({ data, scope });\njson2excel.generate();\n```\n\n![20220413194425](https://raw.githubusercontent.com/asasugar/pic-bed/master/imgs/20220413194425.png)\n\n### 方法二\n\n\u003e (示例: example/html/index.html)\n\n1、下载 `dist` 文件夹 `index.umd.js`;\n\n2、`script` 标签引入：\n\n\u003cscript src=\"xx/index.umd.js\"\u003e\u003c/script\u003e\n\n3、`index.html` 中使用：\n\n```html\nconst data = [ ... ]; const json2excel = new CustomJson2excel.default({ data, keyMap });\njson2excel.generate();\n```\n\n## 导入 Excel 生成 json 数据\n\n`Antd` 例子:\n\n```tsx\nimport { excelToJson } from '@asasugar-use/custom-json2excel';\n\nconst handleImport = async (e: { file: UploadFile; }) =\u003e {\nconst rawFile = e?.file?.originFileObj;\n\nif (!rawFile) return;\n  const json = await excelToJson(rawFile);\n  console.log('%c [ e ]-73', 'font-size:13px; background:pink; color:#bf2c9f;', json);\n};\n\nreturn (\n  \u003cUpload name=\"file\" accept=\".xlsx, .xls, .csv\" onChange={handleImport}\u003e\n    \u003cButton type='primary'\u003e\n      导入excel， 生成json\n    \u003c/Button\u003e\n  \u003c/Upload\u003e\n)\n```\n\n## Props type\n\n| Prop       | Type         | Defaults | Required | Description                                                        |\n| :--------- | :----------- | :------- | :------- | :----------------------------------------------------------------- |\n| data       | Array        | []       | ✓        | 转化成表格初始 json 数据                                           |\n| orderedKey | Array        | []       | ×        | 按照 key 顺序返回列，不在数组中的字段将自动过滤，优先级大于 `filters` |\n| filters    | Array        | []       | ×        | 需要过滤的字段数组,适用于需过滤的数据较少                          |\n| keyMap     | Object       | {}       | ×        | keyMap 映射表，用于自定义表格列名称                                |\n| name       | String       | excel    | ×        | excel 表格名称                                                     |\n| title      | Array        | []       | ×        | 表格标题名称 {name:String,colspan:Number} name:名称， colspan:列数   |\n| footer     | Array        | []       | ×        | 表格最后一列名称，参数同 title                                       |\n| type       | String       | xls      | ×        | 生成的表格类型，可选值(xls、csv)                                     |\n| scope      | Object/String |          | ×        | 渲染的数据层级较深时扁平化处理                                       |\n| onStart    | Function     |          | ×        | 生成 Excel 前的回调函数                                              |\n| onSuccess  | Function     |          | ×        | 生成 Excel 成功的回调函数                                            |\n| onError    | Function     |          | ×        | 生成 Excel 失败的回调函数                                            |\n\n## Stargazers\n[![Stargazers repo roster for @asasugar/custom-json2excel](https://reporoster.com/stars/asasugar/custom-json2excel)](https://github.com/asasugar/custom-json2excel/stargazers)\n\n## Forkers\n[![Forkers repo roster for @asasugar/custom-json2excel](https://reporoster.com/forks/asasugar/custom-json2excel)](https://github.com/asasugar/custom-json2excel/network/members)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasasugar%2Fcustom-json2excel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasasugar%2Fcustom-json2excel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasasugar%2Fcustom-json2excel/lists"}