ExcelCellReference Class
Definition
- 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
columnIndexintThe zero-based column index.
Returns
- string
The column name, for example
Afor index0orAAfor index26.
Exceptions
- ArgumentOutOfRangeException
Thrown when
columnIndexis negative.
ToA1(int, int)
Converts zero-based cell coordinates to their A1 reference.
public static string ToA1(int rowIndex, int columnIndex)
Parameters
Returns
- string
The A1 reference, for example
A1for row0, column0.
Examples
using Bodu.Formats.Excel;
string a1 = ExcelCellReference.ToA1(rowIndex: 0, columnIndex: 0); // "A1"
Exceptions
- ArgumentOutOfRangeException
Thrown when
rowIndexorcolumnIndexis 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
referencestringThe A1 reference, for example
A1orab10.rowIndexintWhen this method returns, the zero-based row index when parsing succeeds.
columnIndexintWhen this method returns, the zero-based column index when parsing succeeds.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |