2

I have a scenario while working with Microsoft Excel Interop.

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

I am operating on Worksheets, so i can not have Chart in this. What i want to do achieve is operate on Sheets and check if it is a Worksheet or Chart, and act accordingly.

As sheets contain both Worksheet, Chart and "Excel 4.0 Macro", so what is the type of Sheets each entry as it can hold any of the type mentioned.

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

2 回答 2

1

通过检查当前枚举器的类型来解决它

var item = wsEnumerator.Current;                   
if (item is Excel.Chart)
{
    //Do chart operations
}
else if (item is Excel.Worksheet)
{
    //Do sheetoperations
}
于 2013-04-25T13:37:10.343 回答
0

您可以重写整个代码(没有枚举器):

foreach (dynamic sheet in workBookIn.Sheets)
{
    if (sheet is Chart)
        // Write your code
    else if (sheet is Worksheet)
        // Write your code
}
于 2019-03-25T12:03:55.100 回答