在javaScript中使用ActiveX to array获取工作表名称列表

时间:2013-03-26 11:30:06

标签: javascript excel activex activexobject

如何使用ActiveX对象获取excel文件的所有可用工作表名称列表?

我知道这段代码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;             

}

这是我现在的代码.....

2 个答案:

答案 0 :(得分:2)

好的,经过大量的反复试验后我得到了一个有效的答案:

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

答案 1 :(得分:0)

由于Sheets或Worksheets集合中的简单选项不多,您可以进行简单的循环:

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

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