获取一个 Microsoft.Office.Interop.Excel.Range,它表示 NamedRange 控件中的一列或多个列。
命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)
语法
声明
ReadOnly Property Columns As Range
    Get
Range Columns { get; }
属性值
类型:Microsoft.Office.Interop.Excel.Range
Microsoft.Office.Interop.Excel.Range ,它表示 NamedRange 控件中的一列或多个列。
备注
当不带参数使用时,此属性将返回包含已知范围内所有列的 Range 对象。
此属性可以与下列可选参数搭配,用于获取指定范围中的特定列。 如果使用此属性时带有参数,则返回值是一个必须强制转换为 Range 的对象。
| Parameter | 说明 | 
|---|---|
| RowIndex | 要获取的一列或多列的索引。 若要获取单一列,请将下列对象之一传递给此参数: 
 若要获取多个连续的列,必须以格式“first column letter:last column letter”传递字符串。 例如,要获取 A 到 E 列,请传递“A:E”。 .gif) 注意 
此参数的名称容易产生误解;此参数指定您要获取的列(而不是行)的索引。
 | 
| ColumnIndex | 不要使用此参数。 如果尝试向该参数传递值,该属性将会引发 COMException。 | 
示例
下面的代码示例创建一个 NamedRange,然后使用 Columns 属性来设置范围内第一列中所有单元格的字体颜色、名称、大小和加粗程度。
此示例针对的是文档级自定义项。 若要进行此代码,请将它复制到项目中的一个工作表类中。
Private Sub SetFirstColumnFont()
    Dim testRange As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1", "J10"), _
        "TestRange")
    testRange.Select()
    Dim fillColumn As Excel.Range = TryCast(testRange.Columns("A"), Excel.Range)
    With fillColumn.Font
        ' Set the font color to blue (RGB value 00 00 FF), and set other font properties.
        .Color = &HFF0000
        .Name = "Arial"
        .Size = 14
        .Bold = False
    End With
    ' Test the changes by writing a value to all the column cells.
    fillColumn.Value2 = "This is a test"
End Sub
private void SetFirstColumnFont()
{
    Microsoft.Office.Tools.Excel.NamedRange testRange = 
        this.Controls.AddNamedRange(this.Range["A1", "J10"],
        "TestRange");
    testRange.Select();
    Excel.Range fillColumn = (Excel.Range)testRange.Columns["A", missing];
    Excel.Font columnsFont = fillColumn.Font;
    // Set the font color to blue (RGB value 00 00 FF), and set other font properties.
    columnsFont.Color = 0xFF0000;
    columnsFont.Name = "Arial";
    columnsFont.Size = 14;
    columnsFont.Bold = false;
    // Test the changes by writing a value to all the column cells.
    fillColumn.Value2 = "This is a test";
}
下面的代码示例创建一个 NamedRange,然后使用 Columns 属性来确定范围中有多少列。
此示例针对的是文档级自定义项。 若要进行此代码,请将它复制到项目中的一个工作表类中。
Private compositeRange As Microsoft.Office.Tools.Excel.NamedRange
Private Sub DisplayRangeComposition()
    compositeRange = Me.Controls.AddNamedRange( _
        Me.Range("B2", "E5"), "compositeRange")
    compositeRange.Cells.Interior.Color = &HFF00
    MessageBox.Show("The range has " & _
        compositeRange.Count & " cells.")
    MessageBox.Show("The range has " & _
        compositeRange.Columns.Count & " columns.")
    MessageBox.Show("The range has " & _
        compositeRange.Rows.Count & " rows.")
End Sub
Microsoft.Office.Tools.Excel.NamedRange compositeRange;
private void DisplayRangeComposition()
{
    compositeRange = this.Controls.AddNamedRange(
        this.Range["B2", "E5"], "compositeRange");
    compositeRange.Cells.Interior.Color = 0xFF00;
    MessageBox.Show("The range has " + compositeRange.Count + 
        " cells.");
    MessageBox.Show("The range has " + 
        compositeRange.Columns.Count + " columns.");
    MessageBox.Show("The range has " + 
        compositeRange.Rows.Count + " rows.");
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。