Worksheet.Range 属性

获取一个 Microsoft.Office.Interop.Excel.Range 对象,该对象表示一个单元格或一定单元格范围。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
ReadOnly Property Range As Worksheet_RangeType
    Get
Worksheet_RangeType Range { get; }

属性值

类型:Microsoft.Office.Tools.Excel.Worksheet_RangeType
一个 Microsoft.Office.Interop.Excel.Range 对象,表示一个单元格或单元格范围。

备注

Range 属性适合与下面的参数一起使用。

Parameter

说明

Cell1

采用应用程序语言的以 A1 样式表示法表示的范围名称。 范围名称可以包括范围运算符(冒号)、交集运算符(空格)或并集运算符(逗号)。 范围名称还可以包括货币符号,但是这些符号被忽略。 可以在范围的任何部分中使用本地定义的名称。 如果使用一个名称,则假定该名称使用的是应用程序的语言。 此参数是必需的。

Cell2

范围右下角的单元格。 可以是一个包含单个单元格、整列、整行的 Microsoft.Office.Interop.Excel.Range,或者可以是一个以应用程序的语言命名单个单元格的字符串。 此参数是可选的。

如果使用 Range 属性时没有指定任何参数,则会从代码返回一个不适合直接使用的 Worksheet_RangeType 对象。

可选参数

有关可选参数的信息,请参见Office 解决方案中的可选参数

示例

下面的代码示例演示使用 Range 属性访问单个单元格或多个单元格的不同方式。

此示例针对的是应用程序级外接程序。

Private Sub CompareRangeUsage()

    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Globals.ThisAddIn.Application.ActiveWorksheet("Sheet1")
    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
        Globals.Factory.GetVstoObject(NativeWorksheet)

    ' The following line of code specifies a single cell.
    vstoWorksheet.Range("A1").Value2 = "Range 1"

    ' The following line of code specifies multiple cells.
    vstoWorksheet.Range("A3", "B4").Value2 = "Range 2"

    ' The following line of code uses an Excel.Range for 
    ' the second parameter of the Range property.
    Dim range1 As Excel.Range = vstoWorksheet.Range("C8")
    vstoWorksheet.Range("A6", range1).Value2 = "Range 3"
End Sub
private void CompareRangeUsage()
{                      
    Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(
        this.Application.ActiveWorkbook.Worksheets[1]);
    // The following line of code specifies a single cell.
    vstoWorksheet.Range["A1", missing].Value2 = "Range 1";

    // The following line of code specifies multiple cells.
    vstoWorksheet.Range["A3", "B4"].Value2 = "Range 2";

    // The following line of code uses an Excel.Range for 
    // the second parameter of the Range property.
    Excel.Range range1 = vstoWorksheet.Range["C8", missing];
    vstoWorksheet.Range["A6", range1].Value2 = "Range 3";
}

.NET Framework 安全性

请参见

参考

Worksheet 接口

Microsoft.Office.Tools.Excel 命名空间