javascriptnode.jsexcelxlsxsheetjs

How to create a new sheet in an existing xlsx file using xlsx/sheetjs package in node js?


How to create/ add a new sheet file in an already existing xlsx file using the xlsx package by sheetjs in Node js?

This is my code by far, for an existing "todo-list.xlsx" file.

const xlsx = require('xlsx');
workBook = xlsx.readFile("todo-list.xlsx", {cellDates:true});

I would like to do something like this (this is done by using the Excel JS package):

const sheet = workbookStream.addSheet('sheet1');

... but using the xlsx package.

Thank you very much in advanced!


Solution

  • There is an example of adding a new worksheet to an existing workbook In the official documentation:

    var ws_name = "SheetJS";
     
    /* make worksheet */
    var ws_data = [
      [ "S", "h", "e", "e", "t", "J", "S" ],
      [  1 ,  2 ,  3 ,  4 ,  5 ]
    ];
    var ws = XLSX.utils.aoa_to_sheet(ws_data);
     
    /* Add the worksheet to the workbook */
    XLSX.utils.book_append_sheet(wb, ws, ws_name);
    

    See Working with the Workbook