How to create an Excel File with Nodejs?

前端 未结 10 896
攒了一身酷
攒了一身酷 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:28

    Using fs package we can create excel/CSV file from JSON data.

    Step 1: Store JSON data in a variable (here it is in jsn variable).

    Step 2: Create empty string variable(here it is data).

    Step 3: Append every property of jsn to string variable data, while appending put '\t' in-between 2 cells and '\n' after completing the row.

    Code:

    var fs = require('fs');
    
    var jsn = [{
        "name": "Nilesh",
        "school": "RDTC",
        "marks": "77"
       },{
        "name": "Sagar",
        "school": "RC",
        "marks": "99.99"
       },{
        "name": "Prashant",
        "school": "Solapur",
        "marks": "100"
     }];
    
    var data='';
    for (var i = 0; i < jsn.length; i++) {
        data=data+jsn[i].name+'\t'+jsn[i].school+'\t'+jsn[i].marks+'\n';
     }
    fs.appendFile('Filename.xls', data, (err) => {
        if (err) throw err;
        console.log('File created');
     });
    

提交回复
热议问题