2015-06-16 17 views
21

Ok, quindi ho trovato questo molto ben documentato node_module chiamato js-xlsxParse XLSX con nodo e creare JSON

Domanda: Come possoanalizzare un xlsx in uscita JSON?

Ecco ciò che il foglio di Excel si presenta come:

enter image description here

Alla fine il JSON dovrebbe essere simile a questo:

[ 
    { 
    "id": 1, 
    "Headline": "Team: Sally Pearson", 
    "Location": "Austrailia", 
    "BodyText": "...", 
    "Media: "..." 
    }, 
    { 
    "id": 2, 
    "Headline": "Team: Rebeca Andrade", 
    "Location": "Brazil", 
    "BodyText": "...", 
    "Media: "..." 
    } 
] 

index.js:

if(typeof require !== 'undefined') { 
    console.log('hey'); 
    XLSX = require('xlsx'); 
} 
var workbook = XLSX.readFile('./assets/visa.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; 
    // console.log(y + "!" + z + "=" + JSON.stringify(worksheet[z].v)); 

    } 

}); 
XLSX.writeFile(workbook, 'out.xlsx'); 
+0

Cosa stai cercando di raggiungere? Se vuoi semplicemente condividere i fogli Excel nella struttura JSON, o semplicemente creare fogli Excel basati su cloud per una facile condivisione, ci sono molti prodotti là fuori che fanno queste cose. Ad esempio https://www.ipushpull.com – Tom

+0

Costo. E so che questo lib farà il semplice compito a portata di mano –

+0

C'è anche la versione di prova, ma in pratica dovrai pagare se vuoi usare qualcosa di più user friendly della libreria raw. Dalla tua domanda non è chiaro che cosa stai cercando di raggiungere o qual è il problema. Stai cercando di aggiungere la prima riga al json finale? – Tom

risposta

20

versione migliorata di "Josh Marinacci" risposta, leggerà al di là della colonna Z (cioè AA1).

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 tt = 0; 
     for (var i = 0; i < z.length; i++) { 
      if (!isNaN(z[i])) { 
       tt = i; 
       break; 
      } 
     }; 
     var col = z.substring(0,tt); 
     var row = parseInt(z.substring(tt)); 
     var value = worksheet[z].v; 

     //store header names 
     if(row == 1 && value) { 
      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); 
}); 
19

Penso che questo codice farà a te lo vuoi Memorizza la prima riga come un insieme di intestazioni, quindi memorizza il resto in un oggetto dati che è possibile scrivere su disco come 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); 
}); 

stampe fuori

[ { 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/' } ] 
+0

Dolce Ci proverò stasera –

+0

Sei l'uomo Josh! Ho apportato alcune modifiche al salvataggio di un file. Goditi la tua taglia –

28

È anche possibile utilizzare

var XLSX = require('xlsx'); 
var workbook = XLSX.readFile('Master.xlsx'); 
var sheet_name_list = workbook.SheetNames; 
console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]])) 
+1

Perfetto, elegante e breve! – user3139868