2013-04-25 68 views
1

我在使用Microsoft Excel Interop時遇到了一個情況。Excel Interop,爲工作表和圖表迭代工作簿

System.Collections.IEnumerator wsEnumerator = excelApp.ActiveWorkbook.Worksheets.GetEnumerator(); 
while (wsEnumerator.MoveNext()) 
{ 
    wsCurrent = (Excel.Worksheet)wsEnumerator.Current; 
    //Worksheet operation Follows 
} 

我在工作表上操作,所以我不能在這個圖表。我想要做的是在表格上進行操作,並檢查它是否爲工作表或圖表,並據此採取行動。由於工作表同時包含工作表,「圖表」和「Excel 4.0宏」,因此每個條目的表格類型是什麼,因爲它可以包含任何提及的類型。

System.Collections.IEnumerator wsEnumerator = workBookIn.Sheets.GetEnumerator(); 
while (wsEnumerator.MoveNext()) 
{ 
    //Identify if its a chart or worksheet 
} 

回答

1

通過檢查當前枚舉的類型解決了它

var item = wsEnumerator.Current;     
if (item is Excel.Chart) 
{ 
    //Do chart operations 
} 
else if (item is Excel.Worksheet) 
{ 
    //Do sheetoperations 
} 
相關問題