Search code examples
javascriptnode.jsexpresselectronexceljs

Add a new record to an existing records contained excel file in excel js( already the excel contains some value now trying to insert a new record )


1 -I want to add a new record inside the excel which is already contains some value 2 - Is there any way to use excel as the database for our project

so that client can use the excel effieciently

//script file.js

          var  Excel = require('exceljs');
          var workbook = new Excel.Workbook();

        //calling 2 function (writeFile() and writeFile1() )
          writeFile();
          writeFile1();
         // this function should add/ create the record in excel file  
          function writeFile(){

                    var worksheet = workbook.addWorksheet('sheet1');
                     worksheet.columns =[
                     {header:"Id",key:"id",width:10},
                     {header:'Type',key:'type',width:15},
                     {header:'Assigned Engineer',key:'eng',width:25},
                     {header:'Due Date',key:'ddate',width:18},
                     {header:'Client Name',key:'cname',width:20},
                     {header:'person Name',key:'pname',width:20},
                     {header:'enquiry type',key:'etype',width:18},
                     {header:'acknowledge',key:'ack',width:20}
                      ]



    Worksheet.addRow({id:16,type:"Trading1221",eng:"Dhanasekar122",ddate:new 
        Date(),cname:"Ford22",pname:"sekar22",etype:"pipeling2",ack:"Y2"})
    worksheet.addRow({id:71,type:"Trading3221",eng:"Dhanasekar322",ddate:new 
    Date(),cname:"Ford32",pname:"sekar32",etype:"pipeling3",ack:"Y3"})

    workbook.xlsx.writeFile('file2.xlsx').then(function(){

     })
        }

      //similary this below function should also add the record inside the  
       // excel
     function writeFile1(){
      var worksheet = workbook.addWorksheet('sheet1');
                     worksheet.columns =[
                     {header:"Id",key:"id",width:10},
                     {header:'Type',key:'type',width:15},
                     {header:'Assigned Engineer',key:'eng',width:25},
                     {header:'Due Date',key:'ddate',width:18},
                     {header:'Client Name',key:'cname',width:20},
                     {header:'person Name',key:'pname',width:20},
                     {header:'enquiry type',key:'etype',width:18},
                     {header:'acknowledge',key:'ack',width:20}
                      ]

  Worksheet.addRow({id:11,type:"Trading1221",eng:"Dhana11sekar122",ddate:new 
        Date(),cname:"Fo12",pname:"sekar122",etype:"pi1peling2",ack:"Y2"})



worksheet.addRow({id:171,type:"Trading31221",eng:"Dhanasekar11322",ddate:new 
    Date(),cname:"For1d32",pname:"sek1ar32",etype:"pipelin1g3",ack:"Y13"})

    workbook.xlsx.writeFile('file2.xlsx').then(function(){

     })
      }

// what happening is value is overwriting and the excel has the last inserted value I had even tried in the second function of removing the columns but still works the same and shows error on some time


Solution

  • At last i found the solution to the above problem

    //file1.js

               var Excel = require('exceljs')
               var workbook = new Excel.Workbook()
               var arr=[]
               workbook.xlsx.readFile('./file4.xlsx')
               .then(function(){
                var worksheet = workbook.getWorksheet(1)
              var row =[
           [   55,"trading","sekar",new Date(2017-02-12),"ashok leyaland",arun",    
              "modeling","Y"],
           [99,"training",new Date(2018-02-13),"tata motors","dhana","reference 
               name","wheldding","Y"]
           ]
    
    
            worksheet.addRows(row)
                 return       workbook.xlsx.writeFile('./file4.xlsx')
               })
    

    // first you need to read the respective excel file and then you need to select the particular worksheet of the workbook(excel file) now you can readfile are write file using any of the form you can choose and update the value of the excel in the form of array or arrays and return the output as file write function