【问题标题】:Parse XLSX and create json with Node解析 XLSX 并使用 Node 创建 json
【发布时间】:2016-09-01 21:41:59
【问题描述】:

我正在使用名为 js-xlsx 的包链接:https://github.com/SheetJS/js-xlsx
问题:如何通过合并解析 xlsx 以输出 json? Here is what the excel sheet looks like: 最后json应该是这样的:

    [
   {
   "Nuber": 1,
   "Department": "part1",
   "Unit": ["check","second","cable"],
   "BeginTime": "1/7:00",
   "EndTime": "2/20:00",
   "Worker": ["Lucy","Jussa","Peter"]
   },
   {
   "Nuber": 2,
   "Department": "part2",
   "Unit": "check",
   "BeginTime": "1/7:00",
   "EndTime": "1/20:00",
   "Worker": "Yu"
   }
]  

test.js:

XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function(y) { /* iterate through sheets */
  var worksheet = workbook.Sheets[y];
  for (z in worksheet) {
    /* all keys that do not begin with "!" correspond to cell addresses */
    if(z[0] === '!') continue;

  }

});

【问题讨论】:

    标签: javascript json node.js excel


    【解决方案1】:
    var XLSX = require('xlsx');
    var workbook = XLSX.readFile('iris_small.xlsx');
    var sheetNames = workbook.SheetNames;
    
    var sheetIndex = 1;
    
    var df = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[sheetIndex-1]]);
    console.log(df);
    

    给予:

    [ { Id: '1',
        'Petal.Length': '1.4',
        'Petal.Width': '0.2',
        Species: 'setosa' },
      { Id: '2',
        'Petal.Length': '1.4',
        'Petal.Width': '0.2',
        Species: 'setosa' },
      { Id: '3',
        'Petal.Length': '1.3',
        'Petal.Width': '0.2',
        Species: 'setosa' },
      { Id: '4',
        'Petal.Length': '3.9',
        'Petal.Width': '1.4',
        Species: 'versicolor' },
      { Id: '5',
        'Petal.Length': '3.5',
        'Petal.Width': '1',
        Species: 'versicolor' },
      { Id: '6',
        'Petal.Length': '4.2',
        'Petal.Width': '1.5',
        Species: 'versicolor' },
      { Id: '7',
        'Petal.Length': '5.4',
        'Petal.Width': '2.3',
        Species: 'virginica' },
      { Id: '8',
        'Petal.Length': '5.1',
        'Petal.Width': '1.8',
        Species: 'virginica' } ]
    

    如您所见,数字被转换为字符串。我不知道是否可以选择将数字保留为数字。使用 Papa Parse 的解决方法:

    var Papa = require('babyparse');
    
    var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetNames[sheetIndex-1]]);
    
    var json = Papa.parse(csv, 
                    {
                        header: true,
                        skipEmptyLines: true,
                        dynamicTyping: true
                    }
                );
    
    console.log(json.data);
    

    现在结果是:

    [ { Id: 1,
        'Petal.Length': 1.4,
        'Petal.Width': 0.2,
        Species: 'setosa' },
      { Id: 2,
        'Petal.Length': 1.4,
        'Petal.Width': 0.2,
        Species: 'setosa' },
      { Id: 3,
        'Petal.Length': 1.3,
        'Petal.Width': 0.2,
        Species: 'setosa' },
      { Id: 4,
        'Petal.Length': 3.9,
        'Petal.Width': 1.4,
        Species: 'versicolor' },
      { Id: 5,
        'Petal.Length': 3.5,
        'Petal.Width': 1,
        Species: 'versicolor' },
      { Id: 6,
        'Petal.Length': 4.2,
        'Petal.Width': 1.5,
        Species: 'versicolor' },
      { Id: 7,
        'Petal.Length': 5.4,
        'Petal.Width': 2.3,
        Species: 'virginica' },
      { Id: 8,
        'Petal.Length': 5.1,
        'Petal.Width': 1.8,
        Species: 'virginica' } ]
    

    【讨论】:

    • 谢谢! XLSX.utils.sheet_to_csv 和 XLSX.utils.sheet_to_json 非常有帮助,但我希望最后一个 json 看起来像 {Worker": ["Lucy","Jussa","Peter"]} 那些工人的名字有相同的字段名称.
    • @sogg 好的,抱歉。我误读了你的问题。我从来没有读过这样的文件,我无能为力。
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多