Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node exceljs reading file

So according to the offical documentation i should be able to read an excel document using:

    // 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());

I have the following document:

enter image description here

What i need to do is basicly to load each row into an object:

var excelObject = {competence1: '', competence2: ''}

And then save it into an array.

However the documentation doesnt give me more on how i might read from this file. It uses a variable called stream however this variable is not explained anywhere.

Does anyone know the plugin and know how i might achieve my goal?

like image 361
Marc Rasmussen Avatar asked Mar 04 '15 20:03

Marc Rasmussen


People also ask

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.

What is Exceljs?

ExcelJS is a JavaScript library for reading, manipulating and writing spreadsheet data in XLSX format.

Does Exceljs support XLS?

exceljs doesn't support xls , which is the older, proprietary format. xlsx is an open standard and just zipped xml, which is easy to support.


2 Answers

var workbook = new Excel.Workbook(); 
workbook.xlsx.readFile(filename)
    .then(function() {
        var worksheet = workbook.getWorksheet(sheet);
        worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
          console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
        });
    });
like image 138
Diogo Cardoso Avatar answered Oct 10 '22 17:10

Diogo Cardoso


And in case you are using file as ArrayBuffer (for example file was read on client with FileReader.readAsArrayBuffer()), then to make it load with this lib you have to do the next:

    let workbook = new Excel.Workbook();
    let stream = new Stream.Readable();
    stream.push(file); // file is ArrayBuffer variable
    stream.push(null);
    workbook.xlsx.read(stream).then((workbook)=> {
         // get worksheet, read rows, etc
    });
like image 29
Statyan Avatar answered Oct 10 '22 15:10

Statyan