Sunday, 27 June 2021

How to save a worksheet from excel as a new excel file using javascript

An excel file with two worksheets in it(TAB1, TAB2).

enter image description here

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();


from How to save a worksheet from excel as a new excel file using javascript

No comments:

Post a Comment