1 Star 4 Fork -1

hibo/json-excel-convert

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Json Excel 转换工具

采用内存优化模式,支持超大记录集,占用极少内存

升级说明

1.0.1

更新说明字段高度

1.0.2

** Excel.class.php **增加前导单引号,防止Excel自动转换单元格数据

安装第三方扩展

npm install xlsxwriter
npm install xlrd

Json文件转Excel

生成测试数据

python demo-make-json-data.py

在当前目录下面生成JSON文件** JsonToExcel.json ** 内容格式如下

{"data": {"column": [{"edit": false, "field": "id", "example": "", "name": "\u6570\u636e\u7f16\u53f7(\u6570\u5b57)", "description": "\u4e0d\u53ef\u7f16\u8f91"}, {"edit": true, "field": "field0", "example": "101,102", "name": "\u7f16\u53f70(\u591a\u9009)", "description": "\u53ef\u9009\u503c:\n101: \u9009\u98791\n102: \u9009\u98792\n103: \u9009\u98793"}, {"edit": true, "field": "field1", "example": "101,102", "name": "\u7f16\u53f71(\u591a\u9009)", "description": "\u53ef\u9009\u503c:\n101: \u9009\u98791\n102: \u9009\u98792\n103: \u9009\u98793"}, {"edit": true, "field": "field2", "example": "101,102", "name": "\u7f16\u53f72(\u591a\u9009)", "description": "\u53ef\u9009\u503c:\n101: \u9009\u98791\n102: \u9009\u98792\n103: \u9009\u98793"}], "width": 20, "description": ["\u6d4b\u8bd5\u8bf4\u660e1", "\u6d4b\u8bd5\u8bf4\u660e2", "\u6d4b\u8bd5\u8bf4\u660e3"], "name": "\u6d4b\u8bd5\u5de5\u4f5c\u7c3f0", "height": 20}, "type": "sheet"}
{"data": {"field8": "value-0-8", "field9": "value-0-9", "field2": "value-0-2", "id": 0}, "type": "row"}

执行转换

python j2x.py -p JsonToExcel.json

在当前目录下生成** JsonToExcel.xlsx **文件

Excel文件转Json文件

执行转换

python x2j.py -p ExcelToJson.xlsx

在当前目录下生成** ExcelToJson.json **文件 内容格式如下

{"field8": "value-0-8", "field9": "value-0-9", "field2": "value-0-2"}
{"field8": "value-1-8", "field9": "value-1-9", "field2": "value-1-2"}

使用PHP封装,实现PHP读取写入大数据

这个不采用PHPExcel的原因是因为PHPExcel太占用内存,实现相应的功能,写入10w条数据,PHPExcel占用内存达到512M以上.而本程序占用内存小于20M

具体实现参照demo.php文件

php demo.php

输出内容如下

string(9) "test.xlsx"
array(1) {
  ["field"]=>
  string(12) "测试内容"
}
array(1) {
  ["field"]=>
  string(12) "测试内容"
}
array(1) {
  ["field"]=>
  string(12) "测试内容"
}

Json格式说明

工作簿(sheet)

{
  "type": "sheet", // 类型,可选为sheet/row
  "data": {
    "name": "test", // 工作簿名称
    "width": 20, // 工作簿列宽度
    "height": 20, // 工作簿行高度
    "description": [ // 工作簿第一行内容,主要用于内容说明
      "说明1", // 第一列内容
      "说明2", // 第二列内容
      "说明3" // 第三列内容
    ],
    "column": [ // 字段列表
      {
        "field": "id", // 字段名称,必填
        "edit": true, // 是否可编辑,如果设置为false,则列背景将被设置为灰色(#EEEEEE)
        "name": "数据编号", // 字段名称 写入第二行
        "description": "说明", // 字段说明 写入第三行
        "example": "参考值" // 字段参考值 写入第四行
      }
    ]
  }
}

数据(row)

{
  "type": "row", // 类型,可选为sheet/row
  "data": { // 数据
    "id": "test" // 字段名 : 数据值
  }
}
The MIT License (MIT) Copyright (c) 2016 AiboW Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Json文件和Excel文件转换工具(仅支持特定格式),支持低内存占用和超大数据量转换 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/aibow/json-excel-convert.git
git@gitee.com:aibow/json-excel-convert.git
aibow
json-excel-convert
json-excel-convert
master

搜索帮助