2

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!

Ariadne R.
  • 452
  • 11
  • 24

1 Answers1

5

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

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • 1
    "Working with the Workbook" link should probably be updated to https://www.npmjs.com/package/xlsx#modifying-workbook-structure – Martin_W Mar 02 '23 at 21:56