How to create an Excel File with Nodejs ?

How to create an Excel File with Nodejs ?

Asked on January 7, 2019 in Excel.
Add Comment


  • 3 Answer(s)

    Try this Best code :

    // Require library
    var excel = require('excel4node');
     
    // Create a new instance of a Workbook class
    var workbook = new excel.Workbook();
     
    // Add Worksheets to the workbook
    var worksheet = workbook.addWorksheet('Sheet 1');
    var worksheet2 = workbook.addWorksheet('Sheet 2');
     
    // Create a reusable style
    var style = workbook.createStyle({
      font: {
        color: '#FF0800',
        size: 12
    },
      numberFormat: '$#,##0.00; ($#,##0.00); -'
    });
     
    // Set value of cell A1 to 100 as a number type styled with paramaters of style
    worksheet.cell(1,1).number(100).style(style);
     
    // Set value of cell B1 to 300 as a number type styled with paramaters of style
    worksheet.cell(1,2).number(200).style(style);
     
    // Set value of cell C1 to a formula styled with paramaters of style
    worksheet.cell(1,3).formula('A1 + B1').style(style);
     
    // Set value of cell A2 to 'string' styled with paramaters of style
    worksheet.cell(2,1).string('string').style(style);
     
    // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
    worksheet.cell(3,1).bool(true).style(style).style({font: {size: 14}});
     
    workbook.write('Excel.xlsx');
    
    Answered on January 7, 2019.
    Add Comment

      create a file with Tabs as delimiters like as to CSV . Save the file with extension .XLS . Then a file can be opened in Excel.

    Use this code :

    var fs = require('fs');
    var writeStream = fs.createWriteStream("file.xls");
     
    var header="Sl No"+"\t"+" Age"+"\t"+"Name"+"\n";
    var row1 = "0"+"\t"+" 21"+"\t"+"Rob"+"\n";
    var row2 = "1"+"\t"+" 22"+"\t"+"bob"+"\n";
     
    writeStream.write(header);
    writeStream.write(row1);
    writeStream.write(row2);
     
    writeStream.close();
    

    This creates the file in XLS file format .

    Answered on January 7, 2019.
    Add Comment

    Try msexcel-builder. and Install it with:

    npm install msexcel-builder
    

    After that :

    // Create a new workbook file in current working-path
    var workbook = excelbuilder.createWorkbook('./', 'sample.xlsx')
     
    // Create a new worksheet with 10 columns and 12 rows
    var sheet1 = workbook.createSheet('sheet1', 10, 12);
     
    // Fill some data
    sheet1.set(1, 1, 'I am title');
    for (var i = 2; i < 5; i++)
      sheet1.set(i, 1, 'test'+i);
     
    // Save it
    workbook.save(function(ok){
      if (!ok)
        workbook.cancel();
      else
        console.log('congratulations, your workbook created');
    });
    
    Answered on January 7, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.