问题
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