0

1 - 我想在已经包含一些值的 excel 中添加一条新记录 2 - 有没有办法使用 excel 作为我们项目的数据库

以便客户可以有效地使用excel

//脚本文件.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(){

     })
      }

// 发生的事情是值被覆盖,并且 excel 具有我什至在删除列的第二个函数中尝试过的最后一个插入值,但仍然工作相同,并在一段时间内显示错误

4

2 回答 2

0

最后我找到了解决上述问题的方法

//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')
           })

//首先您需要读取相应的excel文件,然后您需要选择工作簿的特定工作表(excel文件)现在您可以使用您可以选择的任何形式读取文件写入文件并更新excel的值数组或数组的形式,并将输出作为文件写入函数返回

于 2018-05-02T05:50:51.083 回答
0

excelJS 需要一个对象数组,其中每个对象都指向 excel 中的行,尝试这样做,这应该可以解决您的 pblm

var rows = [{id:11,type:"Trading1221",eng:"Dhana11sekar122",ddate:new Date(),cname:"Fo12",pname:"sekar122",etype:"pi1peling2",ack:"Y2"},
      {id:171,type:"Trading31221",eng:"Dhanasekar11322",ddate:new Date(),cname:"For1d32",pname:"sek1ar32",etype:"pipelin1g3",ack:"Y13"}];

  worksheet.addRows(rows);
于 2018-04-30T14:14:10.447 回答