Search code examples
javascriptexcelactivexactivexobject

Get list of Worksheet names using ActiveX to array in javaScript


How can i get a list of all available Sheet names for my excel file using ActiveX Object?

I know this code Excel.ActiveWorkBook.Sheets returns the sheets... But how could I get the NAMES of these sheets in an array?

function getTabs()
{

    var w =new ActiveXObject("Excel.Application");                      
    var book = w.Workbooks.Open(excelFile);  
    alert(book.name); //here I get the filename of my xls file and it displays ok
    var ExcelSheet= book.Sheets;
    alert(ExcelSheet); //This alerts to 'undefined' 

    w.Quit();
    w=null;             

}

This is my code right now.....


Solution

  • ok, after a lot of trial and error i have come to an answer that works:

        var w =new ActiveXObject("Excel.Application");                      
        var book = w.Workbooks.Open(excelFile);  
        var ExcelSheet= book.Sheets;
        var sheetCount  = ExcelSheet.Count;
        var arrayOfSheets  = new Array();;
        for(var i=0;i<sheetCount;i++){
            alert("Read sheets from file :"+ExcelSheet.Item(i+1).Name);
            arrayOfSheets [i] =ExcelSheet.Item(i+1).Name;
        }