Name Converter
The NameConverter is a static class that provides methods for name conversion (for example, converting cell names to indexes and vice versa).
Methods
The following table lists the available methods:
| Method | Description |
|---|---|
ConvertRowIndexToName | Converts the row index to a name. |
ConvertRowNameToIndex | Converts the row name to an index. |
ConvertColumnIndexToName | Converts the column index to a name. |
ConvertColumnNameToIndex | Converts the column name to an index. |
ConvertCellIndexToName | Converts the cell index to a name. |
TryConvertNamesToCellReferenceRangeExpression | Tries to convert the cell range names to cell reference ranges. |
ConvertCellReferenceToName | Converts the cell reference to a name. |
ConvertCellRangeToName | Converts the cell range to a name. |
TryConvertNameToCellRange | Tries to convert the name to a cell range. |
ConvertCellIndexesToName | Converts the cell indexes to a name. |
ConvertCellNameToIndex | Converts the cell name to a cell index. |
TryConvertCellNameToIndex | Tries to convert the cell name to an index. |
IsValidA1CellName | Determines whether the name of the cell is valid. |
Convert Row Index to Name
The ConvertRowIndexToName method converts the row index to a name.
Example 1: Convert row index 0 to its worksheet row name
int rowIndex = 0;
string rowName = NameConverter.ConvertRowIndexToName(rowIndex);
Convert Row Name to Index
The ConvertRowNameToIndex method converts the row name to an index.
Example 2: Convert row name 1 to its zero-based row index
string rowName = "1";
int rowIndex = NameConverter.ConvertRowNameToIndex(rowName);
Convert Column Index to Name
The ConvertColumnIndexToName method converts the column index to a name.
Example 3: Convert column index 0 to its worksheet column name
int columnIndex = 0;
string columnName = NameConverter.ConvertColumnIndexToName(columnIndex);
Convert Column Name to Index
The ConvertColumnNameToIndex method converts the column name to an index.
Example 4: Convert column name A to its zero-based column index
string columnName = "A";
int columnIndex = NameConverter.ConvertColumnNameToIndex(columnName);
Convert Cell Index to Name
The ConvertCellIndexToName method converts the cell index to a name. This method exposes two overloads.
Example 5: Convert a CellIndex for row 0, column 0 to cell name A1
CellIndex cellIndex = new CellIndex(rowIndex: 0, columnIndex: 0);
string cellName = NameConverter.ConvertCellIndexToName(cellIndex);
Example 6: Convert row 0 and column 0 directly to cell name A1
string cellName = NameConverter.ConvertCellIndexToName(rowIndex: 0, columnIndex: 0);
Try Convert Names to Cell Reference Range Expression
The TryConvertNamesToCellReferenceRangeExpression method tries to convert the cell range names to cell reference ranges.
Example 7: Convert the range name A1:F11 to a CellReferenceRangeExpression
string cellRangesName = "A1:F11";
Worksheet worksheet = workbook.ActiveWorksheet;
int rowIndex = 0;
int columnIndex = 0;
CellReferenceRangeExpression cellReferenceRangeExpression;
bool isConversionToCellReferenceRangeExpressionSuccessful = NameConverter.TryConvertNamesToCellReferenceRangeExpression(cellRangesName, worksheet, rowIndex, columnIndex, out cellReferenceRangeExpression);
Convert Cell Reference to Name
The ConvertCellReferenceToName method converts the cell reference to a name.
Example 8: Convert the from and to cell references from a range expression back to names
CellReference fromCellReference = cellReferenceRangeExpression.CellReferenceRange.FromCellReference;
string fromCellReferenceName = NameConverter.ConvertCellReferenceToName(fromCellReference);
CellReference toCellReference = cellReferenceRangeExpression.CellReferenceRange.ToCellReference;
string toCellReferenceName = NameConverter.ConvertCellReferenceToName(toCellReference);
Convert Cell Range to Name
The ConvertCellRangeToName method converts the cell range to a name.
Example 9: Convert two CellIndex values into the range name A1:F11
CellIndex fromIndex = new CellIndex(rowIndex: 0, columnIndex: 0);
CellIndex toIndex = new CellIndex(rowIndex: 10, columnIndex: 5);
string cellRangeName = NameConverter.ConvertCellRangeToName(fromIndex, toIndex);
Try Convert Name to Cell Range
The TryConvertNameToCellRange method converts the name to a cell range.
Example 10: Convert the range name A1:F11 back to a CellRange
CellIndex fromIndex = new CellIndex(rowIndex: 0, columnIndex: 0);
CellIndex toIndex = new CellIndex(rowIndex: 10, columnIndex: 5);
string cellRangeName = NameConverter.ConvertCellRangeToName(fromIndex, toIndex);
CellRange cellRange;
bool result = NameConverter.TryConvertCellRangeNameToCellRange(cellRangeName, out cellRange);
Convert Cell Indexes to Name
The ConvertCellIndexesToName method converts the cell indexes to a name.
Example 11: Convert four cell indexes directly into the range name A1:F11
string cellRangeName = NameConverter.ConvertCellIndexesToName(fromRowIndex: 0, fromColumnIndex: 0, toRowIndex: 10, toColumnIndex: 5);
Convert Cell Name to Index
The ConvertCellNameToIndex method converts the cell name to a cell index. This method exposes two overloads.
Example 12: Convert cell name A1 to row and column indexes
string cellName = "A1";
int rowIndex;
int columnIndex;
NameConverter.ConvertCellNameToIndex(cellName, out rowIndex, out columnIndex);
Example 13: Convert cell name A1 and capture absolute row and column flags
string cellName = "A1";
int rowIndex;
bool isRowAbsolute;
int columnIndex;
bool isColumnAbsolute;
NameConverter.ConvertCellNameToIndex(cellName, out isRowAbsolute, out rowIndex, out isColumnAbsolute, out columnIndex);
Try Convert Cell Name to Index
The TryConvertCellNameToIndex method tries to convert the cell name to an index. This method exposes two overloads.
Example 14: Try to convert cell name A1 to row and column indexes
string cellName = "A1";
int rowIndex;
int columnIndex;
bool isConversionSuccessful = NameConverter.TryConvertCellNameToIndex(cellName, out rowIndex, out columnIndex);
Example 15: Try to convert cell name A1 and capture absolute row and column flags
string cellName = "A1";
int rowIndex;
bool isRowAbsolute;
int columnIndex;
bool isColumnAbsolute;
bool isConversionSuccessful = NameConverter.TryConvertCellNameToIndex(cellName, out isRowAbsolute, out rowIndex, out isColumnAbsolute, out columnIndex);
Is Valid A1 Cell Name
The IsValidA1CellName method determines whether the name of the cell is valid.
Example 16: Check whether B2 is a valid A1-style cell name
string cellName = "B2";
bool isValidCellName = NameConverter.IsValidA1CellName(cellName);