MyRange.Cells(i,j) 真的是 return 和 Excel.Range 吗?
Does MyRange.Cells(i,j) truly return an Excel.Range?
首先,我强调我在 VSTO / .Net 环境下,使用 Visual Studio 2017。这里没有 VBA。
您有一个 MyRange as Excel.Range = something
对象,您想要引用该区域左上角的单元格。两个选项:
MyRange.Range("A1")
MyRange.Cells(1,1)
但是,尽管文档表明 .Cells()
returns 是一个 Excel.Range
对象,但 Intellisense 不会这样识别它。例如,MyRange.Cells(1,1).Value2
无法从 Intellisense 下拉列表中轻松获得。但是,如果您手动输入 .Value2
,它将没有问题。
我的问题是:这是 Visual Studio 的一些限制,还是在编译和随后的运行时有一些影响?
首先我开始使用.Range("A1")
,但是在处理动态引用时变得很不方便,例如相当于.Cells(i,j)
。
因此,我创建了自己的扩展,它依赖于隐式转换。那样行吗? (第二题)
Module RangeExtensions
''' <summary>
''' Returns a Range object that represents the cell in the specified range.
''' </summary>
<System.Runtime.CompilerServices.Extension()>
Public Function Cell(ByVal MyRange As Excel.Range, ByVal RowIndex As Long, ByVal ColumnIndex As Long) As Excel.Range
Return MyRange.Cells(RowIndex, ColumnIndex)
End Function
''' <summary>
''' Returns a Range object that represents the cell in the specified worksheet.
''' </summary>
<System.Runtime.CompilerServices.Extension()>
Public Function Cell(ByVal MySheet As Excel.Worksheet, ByVal RowIndex As Long, ByVal ColumnIndex As Long) As Excel.Range
Return MySheet.Cells(RowIndex, ColumnIndex)
End Function
End Module
这不是 Visual Studio 的限制,而是 System.Object
类型的属性之一。
rangeReference.Cells
是 Range
类型上的 属性,returns 是 Excel.Range
对象。
rangeReference.Cells(1,1)
是rangeReference.Cells.Item(1,1)
的快捷写法。 Item
是 Range
对象上的默认值 属性。不幸的是,Item
被定义为 Excel 中的 Variant
类型,.Net 使用 System.Object
类型表示 Variant
类型。要让 Intellisense 将 Item
识别为 Range
,需要将其转换为 Range
类型。
示例:
Dim rng As Excel.Range = Sheet1.Range("A1:B4")
Dim rngCells As Excel.Range = rng.Cells
Dim specificCell As Object
specificCell = rngCells(1, 1)
' or
specificCell = rngCells.Item(1, 1)
Dim specificCellRange As Excel.Range = CType(specificCell, Excel.Range)
However, if you do manually type in .Value2, it will work no problem.
这意味着您正在使用允许后期绑定的 Option Strict Off
; 属性 在 运行 时被发现。后期绑定确实会影响性能,因为 Value2
必须 被发现 然后检索。这是通过编译器插入的额外代码来支持 属性 检索来完成的。
首先,我强调我在 VSTO / .Net 环境下,使用 Visual Studio 2017。这里没有 VBA。
您有一个 MyRange as Excel.Range = something
对象,您想要引用该区域左上角的单元格。两个选项:
MyRange.Range("A1")
MyRange.Cells(1,1)
但是,尽管文档表明 .Cells()
returns 是一个 Excel.Range
对象,但 Intellisense 不会这样识别它。例如,MyRange.Cells(1,1).Value2
无法从 Intellisense 下拉列表中轻松获得。但是,如果您手动输入 .Value2
,它将没有问题。
我的问题是:这是 Visual Studio 的一些限制,还是在编译和随后的运行时有一些影响?
首先我开始使用.Range("A1")
,但是在处理动态引用时变得很不方便,例如相当于.Cells(i,j)
。
因此,我创建了自己的扩展,它依赖于隐式转换。那样行吗? (第二题)
Module RangeExtensions
''' <summary>
''' Returns a Range object that represents the cell in the specified range.
''' </summary>
<System.Runtime.CompilerServices.Extension()>
Public Function Cell(ByVal MyRange As Excel.Range, ByVal RowIndex As Long, ByVal ColumnIndex As Long) As Excel.Range
Return MyRange.Cells(RowIndex, ColumnIndex)
End Function
''' <summary>
''' Returns a Range object that represents the cell in the specified worksheet.
''' </summary>
<System.Runtime.CompilerServices.Extension()>
Public Function Cell(ByVal MySheet As Excel.Worksheet, ByVal RowIndex As Long, ByVal ColumnIndex As Long) As Excel.Range
Return MySheet.Cells(RowIndex, ColumnIndex)
End Function
End Module
这不是 Visual Studio 的限制,而是 System.Object
类型的属性之一。
rangeReference.Cells
是 Range
类型上的 属性,returns 是 Excel.Range
对象。
rangeReference.Cells(1,1)
是rangeReference.Cells.Item(1,1)
的快捷写法。 Item
是 Range
对象上的默认值 属性。不幸的是,Item
被定义为 Excel 中的 Variant
类型,.Net 使用 System.Object
类型表示 Variant
类型。要让 Intellisense 将 Item
识别为 Range
,需要将其转换为 Range
类型。
示例:
Dim rng As Excel.Range = Sheet1.Range("A1:B4")
Dim rngCells As Excel.Range = rng.Cells
Dim specificCell As Object
specificCell = rngCells(1, 1)
' or
specificCell = rngCells.Item(1, 1)
Dim specificCellRange As Excel.Range = CType(specificCell, Excel.Range)
However, if you do manually type in .Value2, it will work no problem.
这意味着您正在使用允许后期绑定的 Option Strict Off
; 属性 在 运行 时被发现。后期绑定确实会影响性能,因为 Value2
必须 被发现 然后检索。这是通过编译器插入的额外代码来支持 属性 检索来完成的。