Skip to content

Instantly share code, notes, and snippets.

@thelbouffi
Last active October 6, 2016 14:27
Show Gist options
  • Save thelbouffi/441cdcce9f9c667dbe962b0adae21ef2 to your computer and use it in GitHub Desktop.
Save thelbouffi/441cdcce9f9c667dbe962b0adae21ef2 to your computer and use it in GitHub Desktop.
Node.js parser of xlsx files
var XLSX = require('xlsx');
var workbook = XLSX.readFile('fileName.xlsx');
var sheet_names = workbook.SheetNames;//array of sheet names
//console.log(sheet_names);
var worksheet = workbook.Sheets;
for(x in sheet_names) {
worksheet = workbook.Sheets[sheet_names[x]];
//console.log(worksheet);
var header = []; //initialize header array
var data = []; //initialize data array
for(y in worksheet) {
//console.log(y[0]);
//console.log(y[1]);
if (y[0] === '!') continue;
var col = y[0];
//console.log(col);
var row = y[1];
//console.log(row);
var value = worksheet[y].v;
//console.log(worksheet[y]);
//console.log(value);
if (row == 1) {
header[col] = value;
continue;
}
if (!data[row]) data[row] = {};
data[row][header[col]] = value;
}
data.shift();
data.shift();
console.log(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment