سؤال

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.....

هل كانت مفيدة؟

المحلول

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;
    }

نصائح أخرى

Since there are not many easy options in Sheets or Worksheets collections, you can do a simple loop:

declare string array with Excel.ActiveWorkBook.Worksheets.Count items.

foreach (Worksheet WS in Excel.ActiveWorkBook.Worksheets)
{
    Add the WS.Name to the array.
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top