This is a great, simple and lovely module for Node.js to parse Excel files.
Install the module:
npm install --save excel
Use it like this:
var xls = require('excel');
xls('Sheet.xlsx', function(err, data) {
if(err) throw err;
// data is an array of arrays
});
As it says, the data it returns is an array of arrays. We want it to be JSON, so that we can do whatever we want with it.
This is a function that converts an array of arrays to JSON:
function convertToJSON(array) {
var first = array[0].join()
var headers = first.split(',');
var jsonData = [];
for ( var i = 1, length = array.length; i < length; i++ )
{
var myRow = array[i].join();
var row = myRow.split(',');
var data = {};
for ( var x = 0; x < row.length; x++ )
{
data[headers[x]] = row[x];
}
jsonData.push(data);
}
return jsonData;
};
Then:
xlsx('tasks.xlsx', function(err,data) {
if(err) throw err;
//console.log(jsonDataArray(data));
console.log(JSON.stringify(convertToJSON(data)));
//console.log(data);
});
It's almost embarrassing how Node enables me to use other people's modules to create apps in such a simple way.
Hi – my data always comes back [] (blank…)
@Dave: Did you notice this: “Only supports xlsx for now.” might be that you’re using an older version of excel documents.
@Jacqie Thank you for this example, just what I was looking for 🙂 Dutch helping the Dutch.
If a value has a comma “,” it is splitting that also. Please, can you modify your code?
I need to conver buffer data which is coming from front-end, can you help.