Class ColumnCollection

ColumnCollection class

Collection of the Column objects that represent the individual column(setting)s in a worksheet. The Column object only represents the settings such as column width, styles, .etc. for the whole column, has nothing to do with the fact that there are non-empty cells(data) or not in corresponding column. And the “Count” of this collection only represents the count Column objects that have been instantiated in this collection, has nothing to do with the fact that there are non-empty cells(data) or not in the worksheet.

public class ColumnCollection : CollectionBase<Column>

Properties

NameDescription
Capacity { get; set; }
Count { get; }
Item { get; }Gets a Column object by column index. The Column object of given column index will be instantiated if it does not exist before.
Item { get; set; }

Methods

NameDescription
BinarySearch(Column)
BinarySearch(Column, IComparer<Column>)
BinarySearch(int, int, Column, IComparer<Column>)
Clear()
Contains(Column)
CopyTo(Column[])
CopyTo(Column[], int)
CopyTo(int, Column[], int, int)
Exists(Predicate<Column>)
Find(Predicate<Column>)
FindAll(Predicate<Column>)
FindIndex(Predicate<Column>)
FindIndex(int, Predicate<Column>)
FindIndex(int, int, Predicate<Column>)
FindLast(Predicate<Column>)
FindLastIndex(Predicate<Column>)
FindLastIndex(int, Predicate<Column>)
FindLastIndex(int, int, Predicate<Column>)
GetByIndex(int)(Obsolete.) Gets the column object by the index.
GetColumnByIndex(int)Gets the Column object by the position in the list.
GetEnumerator()
IndexOf(Column)
IndexOf(Column, int)
IndexOf(Column, int, int)
LastIndexOf(Column)
LastIndexOf(Column, int)
LastIndexOf(Column, int, int)
RemoveAt(int)

Examples


[C#]

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];

//Add new Style to Workbook
Style style = workbook.CreateStyle();

//Setting the background color to Blue
style.ForegroundColor = Color.Blue;

//setting Background Pattern
style.Pattern = BackgroundType.Solid;

//New Style Flag
StyleFlag styleFlag = new StyleFlag();

//Set All Styles
styleFlag.All = true;

//Change the default width of first ten columns
for (int i = 0; i < 10; i++)
{
    worksheet.Cells.Columns[i].Width = 20;
}

//Get the Column with non default formatting
ColumnCollection columns = worksheet.Cells.Columns;

foreach (Column column in columns)
{
    //Apply Style to first ten Columns
    column.ApplyStyle(style, styleFlag);
}

//Saving the Excel file
workbook.Save("book1.xls");

[VB.NET]

'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()

'Obtaining the reference of the first worksheet
Dim worksheet As Worksheet = workbook.Worksheets(0)

'Add new Style to Workbook
Dim style As Style = workbook.CreateStyles()

'Setting the background color to Blue
style.ForegroundColor = Color.Blue

'setting Background Pattern
style.Pattern = BackgroundType.Solid

'New Style Flag
Dim styleFlag As New StyleFlag()

'Set All Styles
styleFlag.All = True

'Change the default width of first ten columns
For i As Integer = 0 To 9
    worksheet.Cells.Columns(i).Width = 20
Next i

'Get the Column with non default formatting
Dim columns As ColumnCollection = worksheet.Cells.Columns

For Each column As Column In columns
    'Apply Style to first ten Columns
    column.ApplyStyle(style, styleFlag)
Next column

'Saving the Excel file
workbook.Save("book1.xls")

See Also