Share via


NamedRange.Rows 屬性

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

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

語法

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

屬性值

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

備註

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

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

參數

描述

RowIndex

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

若要取得單一資料列,請傳遞指定您要取得資料列索引的整數。 資料列索引是從 1 開始。

若要取得多個連續的資料列,請使用格式 "first row:last row" 傳遞字串。 例如,要取得資料列 1 到 5,傳遞 "1:5"。

ColumnIndex

請勿使用這個參數。 如果您嘗試將值傳遞給這個參數,這個屬性會擲回 COMException

範例

下列程式碼範例會建立 NamedRange,然後再使用 Rows 屬性來設定範圍中前五個資料列的所有儲存格之字型色彩、名稱、大小及粗細。

這是示範文件層級自訂的範例。 若要執行這個程式碼,請將它複製到專案中的其中一個工作表類別。

Private Sub SetRowsFont()
    Dim testRange As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1", "J10"), _
        "TestRange")
    testRange.Select()

    Dim fillRows As Excel.Range = TryCast(testRange.Rows("1:5"), Excel.Range)
    With fillRows.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 row cells.
    fillRows.Value2 = "This is a test"
End Sub
private void SetRowsFont()
{
    Microsoft.Office.Tools.Excel.NamedRange testRange =
        this.Controls.AddNamedRange(this.Range["A1", "J10"],
        "TestRange");
    testRange.Select();

    Excel.Range fillRows = (Excel.Range)testRange.Rows["1:5"];
    Excel.Font rowsFont = fillRows.Font;

    // Set the font color to blue (RGB value 00 00 FF), and set other font properties.
    rowsFont.Color = 0xFF0000;
    rowsFont.Name = "Arial";
    rowsFont.Size = 14;
    rowsFont.Bold = false;

    // Test the changes by writing a value to all the row cells.
    fillRows.Value2 = "This is a test";
}

下列程式碼範例會建立 NamedRange,然後使用 Rows 屬性來判斷範圍中有多少資料列。

這是示範文件層級自訂的範例。 若要執行這個程式碼,請將它複製到專案中的其中一個工作表類別。

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 命名空間