Parse XLSX with Node and create json

后端 未结 6 830
傲寒
傲寒 2020-12-02 05:05

Ok so I found this really well documented node_module called js-xlsx

Question: How can I parse an xlsx to output json

6条回答
  •  醉酒成梦
    2020-12-02 05:51

    I think this code will do what you want. It stores the first row as a set of headers, then stores the rest in a data object which you can write to disk as JSON.

    var XLSX = require('xlsx');
    var workbook = XLSX.readFile('test.xlsx');
    var sheet_name_list = workbook.SheetNames;
    sheet_name_list.forEach(function(y) {
        var worksheet = workbook.Sheets[y];
        var headers = {};
        var data = [];
        for(z in worksheet) {
            if(z[0] === '!') continue;
            //parse out the column, row, and value
            var col = z.substring(0,1);
            var row = parseInt(z.substring(1));
            var value = worksheet[z].v;
    
            //store header names
            if(row == 1) {
                headers[col] = value;
                continue;
            }
    
            if(!data[row]) data[row]={};
            data[row][headers[col]] = value;
        }
        //drop those first two rows which are empty
        data.shift();
        data.shift();
        console.log(data);
    });
    

    prints out

    [ { id: 1,
        headline: 'team: sally pearson',
        location: 'Australia',
        'body text': 'majority have…',
        media: 'http://www.youtube.com/foo' },
      { id: 2,
        headline: 'Team: rebecca',
        location: 'Brazil',
        'body text': 'it is a long established…',
        media: 'http://s2.image.foo/' } ]
    

提交回复
热议问题