Get list of Worksheet names using ActiveX to array in javaScript

 ̄綄美尐妖づ 提交于 2019-12-23 03:28:12

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/15635942/get-list-of-worksheet-names-using-activex-to-array-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!