共用方式為


WorksheetBase.Columns Property

Definition

Gets a Range object that represents one or more columns on the worksheet.

public:
 property Microsoft::Office::Interop::Excel::Range ^ Columns { Microsoft::Office::Interop::Excel::Range ^ get(); };
public Microsoft.Office.Interop.Excel.Range Columns { get; }
member this.Columns : Microsoft.Office.Interop.Excel.Range
Public ReadOnly Property Columns As Range

Property Value

A Range object that represents one or more columns on the worksheet.

Examples

The following code example uses the Columns property to set the color, name, size, and boldness of the font of all the cells in the first column on the worksheet.

This example is for a document-level customization. To run this code, copy it into one of the worksheet classes in your project.

private void SetFirstColumnFont()
{
    Excel.Range fillColumn = (Excel.Range)this.Columns["A"];
    Excel.Font columnsFont = fillColumn.Font;

    // Set the font color to blue (RGB value FF 00 00), 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";
}
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 FF 00 00), 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

Remarks

When used without parameters, this property returns a Range object that contains all the columns on the worksheet.

This property can be used with the following optional parameters to get specific columns on the worksheet. If you use this property with parameters, the return value is an object that must be cast to a Range.

Parameter Description
RowIndex The index of one or more columns to get.

To get a single column, pass one of the following objects to this parameter:

- An integer that specifies the index of the column you want to get. The column indexes begin at 1.
- A string that consists of the letter of the column you want to get.

To get multiple contiguous columns, pass a string with the format "first column letter:last column letter". For example, to get columns A through E, pass "A:E". Note: The name of this parameter is misleading; this parameter specifies the indexes of the columns you want to get, not the rows.
ColumnIndex Do not use this parameter. This property will throw a COMException if you try to pass a value to this parameter.

Applies to