How to create an Excel File with Nodejs?

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

    I just figured a simple way out . This works -

    Just create a file with Tabs as delimiters ( similar to CSV but replace comma with Tab ). Save it with extension .XLS . The file can be opened in Excel .

    Some code to help --

    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 . It doesnt work if you try XLSX instead of XLS .

提交回复
热议问题