Table of Contents

ExcelCellReference Class

Definition

Namespace
Bodu.Formats.Excel
Assembly
Bodu.Formats.Excel.Binary.dll
Package
Bodu.Formats.Excel.Binary 1.0.0
Source
ExcelCellReference.cs

Converts between zero-based cell coordinates and the spreadsheet A1 reference notation (for example, A1 or AB10).

public static class ExcelCellReference
Inheritance
ExcelCellReference
Inherited Members

Remarks

The notation names a column by a bijective base-26 sequence of letters (A..Z, AA..AZ, and so on) followed by a one-based row number. The conversions are culture-independent and accept upper- or lower-case column letters when parsing.

using Bodu.Formats.Excel;

string reference = ExcelCellReference.ToA1(rowIndex: 9, columnIndex: 27); // "AB10"

if (ExcelCellReference.TryParseA1("AB10", out int row, out int column))
    Console.WriteLine($"row {row}, column {column}"); // row 9, column 27

Methods

ColumnName(int)

Converts a zero-based column index to its A1 column name.

public static string ColumnName(int columnIndex)

Parameters

columnIndex int

The zero-based column index.

Returns

string

The column name, for example A for index 0 or AA for index 26.

Exceptions

ArgumentOutOfRangeException

Thrown when columnIndex is negative.

ToA1(int, int)

Converts zero-based cell coordinates to their A1 reference.

public static string ToA1(int rowIndex, int columnIndex)

Parameters

rowIndex int

The zero-based row index.

columnIndex int

The zero-based column index.

Returns

string

The A1 reference, for example A1 for row 0, column 0.

Examples

using Bodu.Formats.Excel;

string a1 = ExcelCellReference.ToA1(rowIndex: 0, columnIndex: 0); // "A1"

Exceptions

ArgumentOutOfRangeException

Thrown when rowIndex or columnIndex is negative.

TryParseA1(string?, out int, out int)

Attempts to parse an A1 reference into zero-based cell coordinates.

public static bool TryParseA1(string? reference, out int rowIndex, out int columnIndex)

Parameters

reference string

The A1 reference, for example A1 or ab10.

rowIndex int

When this method returns, the zero-based row index when parsing succeeds.

columnIndex int

When this method returns, the zero-based column index when parsing succeeds.

Returns

bool

true when reference is a valid A1 reference.

Applies to

ProductVersions
.NET8, 10