An excel file with two worksheets in it(TAB1, TAB2).
How to save 'TAB1' from the below excel as a new Excel file say tab1.xlsx?
I am trying to work it out with exceljs but not sure how to save it as new file. If it cannot be done with help of exceljs please suggest any other way to get this done.
const ExcelJS = require('exceljs');
const wb = new ExcelJS.Workbook();
//const newwork = new ExcelJS.Workbook();
filename = "abc.xlsx";
const fl = async () => {
wb.xlsx.readFile(filename)
.then(async function () {
wb.eachSheet(async function (worksheet, sheetId) {
console.log("worksheet", worksheet, "sheetid", sheetId);
if(worksheet.name.toLocaleLowerCase() ==='tab1'){
var data = wb.getWorksheet(worksheet.name);
// how to save worksheet as tab1.xlsx
// let dt =await newwork.xlsx.writeFile('tab1.xlsx',data);
}
});
});
}
fl();

var newWorkBook = new ExcelJS.Workbook();2. create a new sheetvar newWorkSheet = newWorkBook.addWorksheet(worksheet.name);3. for each row set values from the original sheet to the new one:worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) { newWorkSheet.getRow(rowNumber).values = row.values; });4. now you would be able to save the workbookawait newWorkBook.xlsx.writeFile(worksheet.name + '.xlsx');newWorkbook.worksheets[0] = worksheet;