Lely Suju
Lely Suju

Reputation: 89

Import Data From Excel To MySql With Node JS

I want to read excel to save in MySQL database by using NodeJS. I do not know what library to use. I want to be able to read excel based on certain rows and columns. Please help me.

Upvotes: 2

Views: 5272

Answers (2)

Sandeep Patel
Sandeep Patel

Reputation: 5148

There are many libraries that you can use :

  1. sheetjs/xlsx
  2. excel.js

etc.

Upvotes: 1

grizlizli
grizlizli

Reputation: 137

There's a great library SheetJS/js-xlsx that provides an API for reading Excel documents.

For example, if you are uploading a file, you'll end up with something like this:

var XLSX = require('xlsx');

function importFromExcel(file) {
    var reader = new FileReader();
    reader.onload = function (e) {
        /* read workbook */
        var bstr = e.target.result;
        var workbook = XLSX.read(bstr, { type: 'binary' });
        /* for each sheet, grab a sheet name */
        workbook.SheetNames.forEach(function (workSheetName, index) {
            var sheetName = workbook.SheetNames[index];
            var workSheet = workbook.Sheets[sheetName];
            var excelData = (XLSX.utils.sheet_to_json(workSheet, { header: 1 }));
            mapExcelData(excelData); // do whatever you want with your excel data
        });
    };
    reader.readAsBinaryString(file);
}

function mapExcelData(data) {...} 

Upvotes: 0

Related Questions