Share via


NamedRange.Columns 屬性

取得 Microsoft.Office.Interop.Excel.Range,表示 NamedRange 控制項中一個或多個欄。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
ReadOnly Property Columns As Range
Range Columns { get; }

屬性值

型別:Microsoft.Office.Interop.Excel.Range
Microsoft.Office.Interop.Excel.Range ,表示 NamedRange 控制項中一個或多個欄。

備註

不搭配參數使用時,這個屬性會傳回 Range 物件,其中包含具名範圍中所有的資料行。

這個屬性可與下列選擇性參數搭配使用,以取得具名範圍內特定的資料行。 如果您使用這個屬性搭配參數,傳回的值會是必須轉型成 Range 的物件。

參數

描述

RowIndex

所要取得的一個或多個資料行索引。

若要取得單一資料行,請將其中一個下列物件傳遞至這個參數:

  • 整數,指定您想要取得之資料行的索引。 資料行索引是從 1 開始。

  • 由您想要取得之資料行的字母所組成的字串。

若要設法連續多個資料行,請使用格式 "first column letter:last column letter" 傳遞字串。 例如,要取得資料行 A 到 E,傳遞 "A:E"。

注意事項注意事項
這個參數的名稱有些誤導;這個參數會指定您想要取得之資料行 (而不是資料列) 的索引。

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"];
    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 安全性

請參閱

參考

NamedRange 介面

Microsoft.Office.Tools.Excel 命名空間