How to get n rows of a large excel sheet in Nodejs

1.1k Views Asked by At

I am getting excel data in buffer format to nodejs. I have to read this data and send the headers and first 1000 records back as a response.

I have tried the below code

This is my router file, where I am using multer

 const express = require("express");
 const router = express.Router();
 var multer  = require('multer')();
 const uploadDocument = require("./RA/UploadDocument")

router.post("/uploadDocument", multer.single('slicedFile'), uploadDocument)

Below is the uploadDocument file where I have concatenated the chunks received from frontend

 const XLSX = require('xlsx') 
 var buffer = [];
buffer.push(req.file.buffer);
if(req.body.isLastBlob==='true'){
    let buf = Buffer.concat(buffer);
    var base64 = (buf.toString('base64'));
    var pre="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,";
    base64 = pre+base64;
    buffer = [];  

    var docdata = base64.split(',').pop();
    var bufdata = Buffer.from(docdata, 'base64');
      
      let wb= XLSX.read(bufdata, {type: "buffer", sheetRows: 1});
      const wsname = wb.SheetNames[0];
      const ws = wb.Sheets[wsname];
      const fileheaders = XLSX.utils.sheet_to_json(ws, { header: 1 });

      let wb_rows= XLSX.read(bufdata, {type: "buffer", sheetRows: 100});
      const wsname_rows = wb_rows.SheetNames[0];
      const ws_rows = wb_rows.Sheets[wsname_rows];
      const rows = XLSX.utils.sheet_to_json(ws_rows);
  }

This code is working fine for small files which have upto 50000 rows. It is giving meheader name and the value like below

[
  {
    'Name': 'Charge',
    'Region': 'Region 1',
    'Country': 'SA',
    'Pin Code': '100000',
     .....
}]

But when I try the same code for large file which is having upto 1000000 records it is giving me incorrect data as below.

[ { '0': 'Charge', '1': 100, '24': 1, '41': 100, '119': 18513, '204': 2186439862, '378': 'Subscription Fees', '18511': 27964203, '40131818': 115, '111716467': 115, __EMPTY: 'SAF', '1/28/22': 'South Africa', 'Add-on Subscription': 44591.0577662037, '1/28/22_1': 204, '2/27/22': 41, '.0000': 70168816 }

0

There are 0 best solutions below