2013-03-26 42 views
1

如何獲取我的使用ActiveX對象的Excel文件的所有可用工作表名稱的列表?使用ActiveX以javaScript中的數組獲取工作表名稱列表

我知道這個代碼Excel.ActiveWorkBook.Sheets返回工作表...但我怎麼能得到這些表中的名單在一個數組?

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;    

} 

這是我的代碼現在.....

回答

1

OK ,經過大量的反覆試驗,我得出了一個可行的答案:

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

由於國內沒有成片或工作表集合了多種方便的選擇,你可以做一個簡單的循環:

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

foreach (Worksheet WS in Excel.ActiveWorkBook.Worksheets) 
{ 
    Add the WS.Name to the array. 
} 
+0

我想用javaScript獲取數組 – MaVRoSCy 2013-03-26 13:08:03

相關問題