How to create an Excel File with Nodejs?

前端 未结 10 886
攒了一身酷
攒了一身酷 2020-11-30 17:17

I am a nodejs programmer . Now I have a table of data that I want to save in Excel File format . How do I go about doing this ?

I found a few Node libraries . But m

10条回答
  •  离开以前
    2020-11-30 17:34

    excel4node is a maintained, native Excel file creator built from the official specification. It's similar to, but more maintained than mxexcel-builder mentioned in the other answer.

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

提交回复
热议问题