0

I am struggeling with understanding the following code

Dim _xlsSheet As Excel.Worksheet = DirectCast(xlworkbook.Worksheets(Name), Excel.Worksheet)

or

xlworkbook = DirectCast(xlApp.ActiveWorkbook, Excel.Workbook)

All examples I work throu concerning Excel.Interop I see the use of DirectCast everywhere. what i dont understand is why we have to use DirectCast here. If I declare dim xlworkbook as excel.workbook have using directcast instead of just assigning the workbook?

4

1 回答 1

1

xlworkbook.Worksheets()方法返回一个Excel.Sheets集合。集合(通常)返回 type 的项目Object,因此,它们应该转换为您期望的类型。

至于xlworkbook = DirectCast(xlApp.ActiveWorkbook, Excel.Workbook),似乎DirectCast不需要,除非 xlApp 是在没有引用 using 的情况下创建的,否则CreateObject("Excel.Application")会创建一个对象。

于 2014-05-06T08:51:22.940 回答