Class RangeColumn
A class that represents a single column of cells.
Inherited Members
Namespace: IronXL
Assembly: IronXL.dll
Syntax
public class RangeColumn : Range
RangeColumn treats one whole column of an Excel sheet as a unit in C#, so a developer can size it, hide it, identify it, or read its cells without listing each one. It is the column-shaped view a worksheet hands back, and it carries the column-level operations that a plain range of arbitrary cells does not. Code reaching for "C# Excel column" wants this type.
A column comes from a worksheet through GetColumn, by index or by letter, which returns the RangeColumn for that position. Because RangeColumn derives from Range, every range member still applies, so the column's cells can be summed, sorted, iterated, or formatted exactly as any other range, while the column-specific members add the operations that only make sense on a full column.
Those members are few and focused. ColumnLetter and ColumnNumber identify the column in spreadsheet terms (A, B, ... or 1, 2, ...), which is the pair developers reach for when mapping between user-facing letters and zero-based code. Width reads or sets how wide the column renders, and AutoSizeColumn fits that width to the content instead, with an overload that controls whether merged cells count toward the measurement. Hidden shows or hides the column, and RemoveColumn deletes it from the sheet, shifting the columns after it. These edits stay in memory until the parent WorkBook is saved, so a column change and the workbook that owns it are written together.
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
RangeColumn column = workSheet.GetColumn(0);
column.AutoSizeColumn();
workBook.SaveAs("output.xlsx");The auto resize how-to covers AutoSizeColumn, the add rows and columns how-to inserts and removes columns, and the select range how-to shows GetColumn.
Properties
ColumnLetter
Gets the letter anme of the column.
Declaration
public string ColumnLetter { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
ColumnNumber
0-based index of the column.
Declaration
public int ColumnNumber { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Hidden
Gets or sets a value indicating whether this column is hidden.
Declaration
public bool Hidden { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Width
Gets or sets the width of the column.
Declaration
public int Width { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Methods
AutoSizeColumn()
Changes width of the column to fit the content based on font measurements.
This method measures the text in the cell based on the font that is used for the cell.
If the exact font is not found on the machine, it will try to measure with a default font (Arial).
------------------------------------------------
Usage:
// Auto-size column B to fit its content
var workbook = WorkBook.Load("example.xlsx");
var sheet = workbook.DefaultWorkSheet;
var column = sheet.GetColumn(1); // Column B (0-indexed)
column.AutoSizeColumn();
workbook.SaveAs("autosized.xlsx");
------------------------------------------------
Declaration
public void AutoSizeColumn()
Remarks
Important Considerations:
⚠️ Font Availability: The exact font must be installed on the system for accurate measurements.
📝 Note: This overload does not consider merged cells. Use AutoSizeColumn(true) to include merged cells.
Related Documentation:📖 How-To Guide:AutoSize Rows and Columns Guide
📚 API Reference:RangeColumn API Documentation
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Thrown when the column cannot be auto-sized. |
| SixLabors.Fonts.FontException | Thrown when no fonts are found installed on the machine. |
AutoSizeColumn(Boolean)
Changes width of the column to fit the content based on font measurements.
This method measures the text in the cell based on the font that is used for the cell.
If the exact font is not found on the machine, it will try to measure with a default font (Arial).
------------------------------------------------
Usage:
// Auto-size column B including merged cells
var workbook = WorkBook.Load("example.xlsx");
var sheet = workbook.DefaultWorkSheet;
var column = sheet.GetColumn(1); // Column B (0-indexed)
column.AutoSizeColumn(true); // Include merged cells
workbook.SaveAs("autosized.xlsx");
------------------------------------------------
Declaration
public void AutoSizeColumn(bool useMergedCells)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | useMergedCells | If true, the width of merged cells will be taken into account. |
Remarks
Important Considerations:
⚠️ Font Availability: The exact font must be installed on the system for accurate measurements.
📝 Merged Cells: When useMergedCells is true, the width calculation considers cells that span multiple columns.
🎯 Long Text: Ensures column width accommodates the longest text content in the column.
Related Documentation:📖 How-To Guide:AutoSize Rows and Columns Guide
📚 API Reference:RangeColumn API Documentation
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Thrown when the column cannot be auto-sized. |
| SixLabors.Fonts.FontException | Thrown when no fonts are found installed on the machine. |
RemoveColumn()
Removes the column from the worksheet.
Declaration
public void RemoveColumn()