how to read an excel file using node.js

2019-09-24 11:37发布

I'm trying to read an excel file present in S3 using node.js integrated with lambda. I mean the code should interact with excel file and display the output. Please help me in this issue.

1条回答
时光不老,我们不散
2楼-- · 2019-09-24 12:41

To read a file from S3 in AWS lambda with nodejs you can follow given steps. To read data from excel file in node js, I prefer xlsx package. To use it, first you need to install xlsx node package and then proceed as given -

 npm i xlsx --save

Then you can read excel file as -

const xlsx = require('xlsx');

var params = {
    Bucket: "",
    Key: ""
};

var file = s3.getObject(params).createReadStream();
var buffers = [];

file.on('data', function (data) {
    buffers.push(data);
});

file.on('end', function () {
    var buffer = Buffer.concat(buffers);
    var workbook = xlsx.parse(buffer);
    console.log("workbook", workbook);

    var sheet_name_list = workbook.SheetNames;
    //if you have multiple sheets
    data = xlsx.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]); 

    for(var key in data){
       console.log(data[key]['yourColumn']);
    }
});
查看更多
登录 后发表回答