how to read an excel file using node.js

久未见 提交于 2019-12-26 14:00:10

问题


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:


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']);
    }
});


来源:https://stackoverflow.com/questions/50848483/how-to-read-an-excel-file-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!