WorksheetBase.Columns 属性

获取一个表示工作表上一个或多个列的 Range 对象。

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

语法

声明
Public ReadOnly Property Columns As Range
    Get
public Range Columns { get; }

属性值

类型:Microsoft.Office.Interop.Excel.Range
一个表示工作表上一个或多个列的 Range 对象。

备注

当不带参数使用时,此属性将返回包含工作表上所有列的 Range 对象。

此属性可以与下列可选参数搭配,用于获取工作表上的特定列。 如果使用此属性时带有参数,则返回值是一个必须强制转换为 Range 的对象。

Parameter

说明

RowIndex

要获取的一列或多列的索引。

若要获取单一列,请将下列对象之一传递给此参数:

  • 指定您要获取的列的索引的整数。 列索引从位置 1 开始。

  • 一个由您想要获取的列的字母组成的字符串。

若要获取多个连续的列,必须以格式“first column letter:last column letter”传递字符串。 例如,要获取 A 到 E 列,请传递“A:E”。

注意注意
此参数的名称容易产生误解;此参数指定您要获取的列(而不是行)的索引。

ColumnIndex

不要使用此参数。 如果尝试向该参数传递值,该属性将会引发 COMException

示例

下面的代码示例使用 Columns 属性设置工作表上第一列中所有单元格的字体的颜色、名称、大小和加粗程度。

此示例针对的是文档级自定义项。 若要进行此代码,请将它复制到项目中的一个工作表类中。

Private Sub SetFirstColumnFont()
    Dim fillColumn As Excel.Range = TryCast(Me.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()
{
    Excel.Range fillColumn = (Excel.Range)this.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";
}

.NET Framework 安全性

请参见

参考

WorksheetBase 类

Microsoft.Office.Tools.Excel 命名空间