ExcelA1
OfficeIMO.Excel
Utility helpers for parsing and converting Excel A1 references. Public so examples and consumers can reuse consistent logic without re-implementing regexes or math.
Inheritance
- object
- ExcelA1
Methods
public static string CellReference(int row, int column) #Builds an A1 cell reference from 1-based row and column indexes.
Parameters
- row int
- column int
Returns
stringpublic static string ColumnIndexToLetters(int index) #Converts a 1-based column index to Excel column letters (e.g., 1→"A", 27→"AA").
Parameters
- index int
- 1-based column index.
Returns
string Excel column letters; returns "A" for non-positive inputs.Examples
string col = ExcelA1.ColumnIndexToLetters(28); // "AB" int idx = ExcelA1.ColumnLettersToIndex("AB"); // 28
public static int ColumnLettersToIndex(string letters) #Converts column letters (e.g., "A", "AA") to a 1-based column index. Non-letter characters are ignored; returns 0 for empty/invalid input.
Parameters
- letters string
- Excel column letters.
Returns
int 1-based column index, or 0 when input yields no letters.public static ValueTuple<int, int> ParseCellRef(string cellRef) #Parses a single A1 cell reference (e.g., "B5") into a 1-based (row, column) tuple. Returns (0,0) when the input does not match a valid simple cell reference.
Parameters
- cellRef string
- A1 cell reference, without sheet prefix.
Returns
ValueTuple<int, int> Tuple of row and column (1-based). Returns (0,0) if invalid.public static ValueTuple<int, int, int, int> ParseRange(string a1Range) #Parses an A1 range (e.g., "A1:B10") into 1-based, normalized bounds. If the bounds are inverted, they are swapped so that r1 <= r2 and c1 <= c2.
Parameters
- a1Range string
- A1 range string, without sheet prefix.
Returns
ValueTuple<int, int, int, int> (r1, c1, r2, c2) 1-based coordinates.Exceptions
ArgumentException– Thrown when the input is not a valid A1 range.
Examples
var (r1, c1, r2, c2) = ExcelA1.ParseRange("B2:D10"); // r1=2, c1=2, r2=10, c2=4
public static bool TryParseRange(string a1Range, out int r1, out int c1, out int r2, out int c2) #Tries to parse an A1 range (e.g., "A1:B10") into 1-based, normalized bounds. Returns false when the input is not a valid A1 range.
Parameters
- a1Range string
- r1 int
- c1 int
- r2 int
- c2 int
Returns
bool