Home > Back-end >  how to read the excel sheet data in node js
how to read the excel sheet data in node js

Time:09-06

Below is my code

var xlsx = require('xlsx');
    var workbook = xlsx.readFile('E:/React application/userResponse.xlsx');
    
    const sheetnames = workbook.SheetNames[0]
    const sheetValue = workbook.Sheets[sheetnames];
    var data = xlsx.utils.sheet_add_json(sheetValue);
    console.log(data);

when I log the data I keep getting the below error

e:\React application\pravinyam-api\node_modules\xlsx\xlsx.js:24131
        var range = ({s: {c:0, r:0}, e: {c:_C, r:_R   js.length - 1   offset}});
                                                         ^

TypeError: Cannot read property 'length' of undefined
    at Object.sheet_add_json (e:\React application\pravinyam-api\node_modules\xlsx\xlsx.js:24131:51)
    at Object.<anonymous> (e:\React application\pravinyam-api\readExcel.js:8:23)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47

If anyone has a better way on how to read the excel sheet or resolve the above issue would of great help! Thanks

CodePudding user response:

Change sheet_add_json with sheet_to_json and follow the path name pattern.

const xlsx = require("xlsx");
const workbook = xlsx.readFile("E:\\React application\\userResponse.xlsx");
const sheetnames = workbook.SheetNames[0];
const data = xlsx.utils.sheet_to_json(workbook.Sheets[sheetnames]);

console.log(data);
  • Related