Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Excel file using node.js

Okay so i am using the FileUploader module to upload my file from angular to my REST API:

var uploader = $scope.uploader = new FileUploader({     url: api.getUrl('uploadCompetence',null) }); 

This is sent to the following POST function:

        router.route('/api/uploadCompetence')         .post(function (req, res) {          // This is where i want to read the file              var competence = Competence.build(req.body.location);             competence.add(function (success) {                     res.json({message: 'quote created!'});                 },                 function (err) {                     res.status(err).send(err);                 });         }) 

Now my goal is to read the excel file and then add each row to my database.

However im not quite sure how i can read the file from Node.js i have debugged my server and couldnt find the file anywhere but the the api is being called from my Angular application

Can anyone push me in the right direction? :)

like image 672
Marc Rasmussen Avatar asked Mar 04 '15 17:03

Marc Rasmussen


People also ask

How read data from excel using JavaScript?

js project, i.e., inside the index. js file. Now, we will create a function parseExcel() , which will take a file filename as a parameter and return an array of objects. Now that we have the excel filename , we can read and extract all the information from that file using the readFile function of the XLSX package.

How do I read a .xlsx file in typescript?

steps to read a value from Excel : // create object for workbook let wb:Workbook = new Workbook(); From the Workbook object, we have to use readFile along with file type, here xlsx is file type. Create then block to resolve the promise to get values of the workbook.


1 Answers

There are a few different libraries doing parsing of Excel files (.xlsx). I will list two projects I find interesting and worth looking into.

Node-xlsx

Excel parser and builder. It's kind of a wrapper for a popular project JS-XLSX, which is a pure javascript implementation from the Office Open XML spec.

node-xlsx project page

Example for parsing file

var xlsx = require('node-xlsx');  var obj = xlsx.parse(__dirname + '/myFile.xlsx'); // parses a file  var obj = xlsx.parse(fs.readFileSync(__dirname + '/myFile.xlsx')); // parses a buffer 

ExcelJS

Read, manipulate and write spreadsheet data and styles to XLSX and JSON. It's an active project. At the time of writing the latest commit was 9 hours ago. I haven't tested this myself, but the api looks extensive with a lot of possibilites.

exceljs project page

Code example:

// read from a file var workbook = new Excel.Workbook(); workbook.xlsx.readFile(filename)     .then(function() {         // use workbook     });  // pipe from stream var workbook = new Excel.Workbook(); stream.pipe(workbook.xlsx.createInputStream()); 
like image 171
aludvigsen Avatar answered Oct 08 '22 04:10

aludvigsen