Back to Devexpress

Worksheet.Columns Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-4f41df22.md

latest18.3 KB
Original Source

Worksheet.Columns Property

Returns the collection of all columns in a worksheet.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
ColumnCollection Columns { get; }
vb
ReadOnly Property Columns As ColumnCollection

Property Value

TypeDescription
ColumnCollection

A ColumnCollection object specifying the worksheet’s collection of columns.

|

Remarks

Use ColumnCollection object members to access, add, hide, group, and remove columns.

Access Columns

Use the ColumnCollection.Item property to access an individual column in a worksheet.

Obtain the Column at the Specified Index

csharp
using DevExpress.Spreadsheet;
// ...

// Access the column collection.
ColumnCollection columns = workbook.Worksheets[0].Columns;

// Access the first column in the collection.
Column firstColumn = columns[0];
vb
Imports DevExpress.Spreadsheet
' ...

' Access the column collection.
Dim columns As ColumnCollection = workbook.Worksheets(0).Columns

' Access the first column in the collection.
Dim firstColumn As Column = columns(0)

A column index is zero-based. It specifies the column position in the collection.

Obtain the Column with a Given Heading

csharp
using DevExpress.Spreadsheet;
// ...

// Access the column collection.
ColumnCollection columns = workbook.Worksheets[0].Columns;

// Access the first column by its heading.
Column firstColumn = columns["A"];
vb
Imports DevExpress.Spreadsheet
' ...

' Access the column collection.
Dim columns As ColumnCollection = workbook.Worksheets(0).Columns

' Access the first column by its heading.
Dim firstColumn As Column = columns("A")

Column headings (“A”, “B”, “C”, etc.) identify each column in a worksheet. Specify column headings in the A1 reference style.

Insert Columns

Use the following methods to insert columns into a worksheet:

Column.InsertInserts a column to the left of the current column.ColumnCollection.InsertInserts columns at the specified position.

Note

The number of columns in a worksheet does not change — 16,384. When you add new columns, other columns in the worksheet are shifted to the right, and an equivalent number of columns at the end of the worksheet is removed.

Insert a Single Column

csharp
// Insert column "B".
worksheet.Columns["B"].Insert();

// Insert column "D".
worksheet.Columns.Insert(3);

// Insert column "F".
worksheet.Columns.Insert("F");
vb
' Insert column "B".
worksheet.Columns("B").Insert()

' Insert column "D".
worksheet.Columns.Insert(3)

' Insert column "F".
worksheet.Columns.Insert("F")

Insert Multiple Columns

csharp
// Insert three columns (from column "B" to column "D").
worksheet.Columns.Insert(1, 3);

// Insert two columns starting with column "F".
worksheet.Columns.Insert("F", 2);
vb
' Insert three columns (from column "B" to column "B").
worksheet.Columns.Insert(1, 3)

' Insert two columns starting with column "F".
worksheet.Columns.Insert("F", 2);

Insert Columns to the Left of a Specific Cell Range

Use the Worksheet.InsertCells method to insert empty columns to the left of the specified cells. The method inserts the same number of columns as the specified cell range.

csharp
// Insert three columns to the left of the "B3:D5" cell range.
worksheet.InsertCells(worksheet.Range["B3:D5"], InsertCellsMode.EntireColumn);
vb
' Insert three columns to the left of the "B3:D5" cell range.
worksheet.InsertCells(worksheet.Range("B3:D5"), InsertCellsMode.EntireColumn)

Format Inserted Columns

Pass a ColumnFormatMode enumeration member to the ColumnCollection.Insert method to specify format options for inserted columns.

The following example inserts two columns (“B” and “C”) and applies the same format as column “A” to these columns:

csharp
// Insert two columns with the same formatting as the column "A".
worksheet.Columns.Insert("B", 2, ColumnFormatMode.FormatAsPrevious);
vb
' Insert two columns with the same formatting as the column "A".
worksheet.Columns.Insert("B", 2, ColumnFormatMode.FormatAsPrevious)

Remove Columns

Use the following methods to delete columns from a worksheet:

Column.DeleteDeletes the current column.ColumnCollection.RemoveDeletes columns at the specified position or deletes multiple columns at once.

Note

The number of columns in a worksheet does not change — 16,384. When you delete columns, other columns in the worksheet are shifted to the left and the equivalent number of new columns is automatically added to the end of the worksheet’s column collection.

Remove a Single Column

csharp
// Delete column "C".
worksheet.Columns.Remove("C");

// Delete the third column.
worksheet.Columns[2].Delete();

// Delete the fifth column.
worksheet.Columns.Remove(4);
vb
' Delete column "C".
worksheet.Columns.Remove("C")

' Delete the third column.
worksheet.Columns(2).Delete()

' Delete the fifth column.
worksheet.Columns.Remove(4)

Remove Multiple Columns

csharp
// Delete three columns (from column "C" to column "E").
worksheet.Columns.Remove("C", "E");
vb
' Delete three columns (from column "C" to column "E").
worksheet.Columns.Remove("C", "E")

Remove Columns That Meet a Specific Condition

csharp
// Specify the condition to remove worksheet columns.
// If text in the first row is "Change in Price", remove the corresponding column. 
Func<int, bool> columnRemovalCondition = x => worksheet.Cells[0, x].Value.TextValue == "Change in Price";

// Delete columns that meet the specified condition.
// Check columns "A" through "H".
worksheet.Columns.Remove(0, 7, columnRemovalCondition);
vb
' Specify the condition to remove worksheet columns.
' If text in the first row is "Change in Price", remove the corresponding column. 
Dim columnRemovalCondition As Func(Of Integer, Boolean) = Function(x) worksheet.Cells(0, x).Value.TextValue = "Change in Price"

' Delete columns that meet the specified condition.
' Check columns "A" through "H".
worksheet.Columns.Remove(0, 7, columnRemovalCondition)

Remove Columns That Contain Specific Cells

Use the Worksheet.DeleteCells method to delete columns that contain the specified cell range.

csharp
// Delete a column that contains the "F3" cell.
worksheet.DeleteCells(worksheet.Cells["F3"], DeleteMode.EntireColumn);
vb
' Delete a column that contains the "F3" cell.
worksheet.DeleteCells(worksheet.Cells("F3"), DeleteMode.EntireColumn)

Group and Ungroup Columns

Use the ColumnCollection.Group method to group columns.

csharp
// Group columns "C" through "F" and expand the group.
worksheet.Columns.Group(2, 5, false);

// Group columns "C" and "D" and collapse the group.
worksheet.Columns.Group("C", "D", true);
vb
' Group columns "C" through "F" and expand the group.
worksheet.Columns.Group(2, 5, False)

' Group columns "C" and "D" and collapse the group.
worksheet.Columns.Group("C", "D", True)

Use the ColumnCollection.UnGroup method to ungroup columns.

csharp
// Ungroup columns "C" through "F".
worksheet.Columns.UnGroup(2, 5, false);

// Ungroup columns "C" and "D" and display collapsed data.
worksheet.Columns.UnGroup("C", "D", true);
vb
' Ungroup columns "C" through "F".
worksheet.Columns.UnGroup(2, 5, False)

' Ungroup columns "C" and "D" and display collapsed data.
worksheet.Columns.UnGroup("C", "D", True)

Expand or Collapse Groups

Use the following methods to collapse or expand column groups in a worksheet:

ColumnCollection.CollapseAllGroupsCollapses all column groups.ColumnCollection.CollapseGroupsCollapses column groups starting with the specified group level.ColumnCollection.ExpandAllGroupsExpands all column groups.

csharp
// Collapse all column groups except the first level group.
worksheet.Columns.CollapseGroups(2);
vb
' Collapse all column groups except the first level group.
worksheet.Columns.CollapseGroups(2)

Hide Columns

Use the Column.Visible property or the ColumnCollection.Hide method to hide columns in a worksheet.

csharp
// Hide column "A".
worksheet.Columns[0].Visible = false;

// Hide columns "C" through "D".
worksheet.Columns.Hide(2, 3);

// Hide columns "F" through "H".
worksheet.Columns.Hide("F", "H");
vb
' Hide column "A".
worksheet.Columns(0).Visible = False

' Hide columns "C" through "D".
worksheet.Columns.Hide(2, 3)

' Hide columns "F" through "H".
worksheet.Columns.Hide("F", "H")

Set the Column.Visible property to true to display a column. Use the ColumnCollection.Unhide method to show multiple hidden columns.

csharp
// Show column "A".
worksheet.Columns[0].Visible = true;

// Show columns "C" through "D".
worksheet.Columns.Unhide(2, 3);

// Show columns "F" through "H".
worksheet.Columns.Unhide("F", "H");
vb
' Show column "A".
worksheet.Columns(0).Visible = True

' Show columns "C" through "D".
worksheet.Columns.Unhide(2, 3)

' Show columns "F" through "H".
worksheet.Columns.Unhide("F", "H")

The following code snippets (auto-collected from DevExpress Examples) contain references to the Columns property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/RowAndColumnActions.cs#L124

csharp
// Copy only borders from the "B" column to the "E" column.
worksheet.Columns["E"].CopyFrom(worksheet.Columns["B"], PasteSpecial.Borders);
#endregion #CopyRowsColumns

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/RowAndColumnActions.cs#L124

csharp
// Copy only borders from the "B" column to the "E" column.
worksheet.Columns["E"].CopyFrom(worksheet.Columns["B"], PasteSpecial.Borders);
#endregion #CopyRowsColumns

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.cs#L114

csharp
// Copy only borders from the "B" column to the "E" column.
worksheet.Columns["E"].CopyFrom(worksheet.Columns["B"], PasteSpecial.Borders);
#endregion #CopyRowsColumns

asp-net-core-spreadsheet-update-document/CS/UpdateDocumentUsingAJAX/Controllers/HomeController.cs#L49

csharp
fistRow.Font.FontStyle = SpreadsheetFontStyle.Bold;
Column fistColumn = worksheet.Columns[0];
fistColumn.ColumnWidthInCharacters = 17;

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/RowAndColumnActions.vb#L126

vb
' Copy only borders from the "B" column to the "E" column.
                worksheet.Columns("E").CopyFrom(worksheet.Columns("B"), PasteSpecial.Borders)
' #End Region ' #CopyRowsColumns

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/RowAndColumnActions.vb#L125

vb
' Copy only borders from the "B" column to the "E" column.
                worksheet.Columns("E").CopyFrom(worksheet.Columns("B"), PasteSpecial.Borders)
' #End Region ' #CopyRowsColumns

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.vb#L92

vb
' Copy only borders from the "B" column to the "E" column.
            worksheet.Columns("E").CopyFrom(worksheet.Columns("B"), PasteSpecial.Borders)
#End Region ' #CopyRowsColumns

See Also

Rows

How to: Access a Row or Column

How to: Add a New Row or Column to a Worksheet

How to: Delete a Row or Column from a Worksheet

How to: Outline Data Manually

Rows and Columns in Spreadsheet Documents

Worksheets in Spreadsheet Documents

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace