I wish to be able to instantiate my Cell
class while naming the cell instance with such name as "A", "B", "C", etc. just like in an Excel spreadsheet.
I have my Cell
class like so:
public class Cell {
public Cell(Range nativeCell) {
NativeCell = nativeCell;
}
public Range NativeCell { get; private set; }
}
And my Sheet
class:
public class Sheet {
private IDictionary<string, Cell> _cells;
public Sheet(Worksheet nativeSheet) {
NativeSheet = nativeSheet;
_cells = new Dictionary<string, Cell>();
for (int rowIndex = 1; rowIndex <= NativeSheet.Rows.Count; ++rowIndex)
for (int colIndex = 1; colIndex <= NativeSheet.Columns.Count; ++colIndex) {
ICell newCell = new Cell(NativeSheet.Cells(rowIndex, colIndex));
newCell.Name = ?? // This name should look like "A1", "B1", "AA3", "CB20", etc.
Cells.Add(newCell.Name, newCell);
}
}
public IDictionary<string, Cell> Cells {
get {
return _cells;
}
}
public Worksheet NativeSheet { get; private set; }
}
I would need to generate a name based on the alphabetic letters and double and triple them once I encounter the last alphabet letter 'Z'. The algorithm would have to generate the letters that I would concatenate with the rowIndex
value that would result to this naming strategy such as Excel.
The letters would be:
A, B, C, D...Z, AA, AB, AC...AZ, BA, BB, BC...BZ, CA...XAA, XAB, XAC...
While we clearly know that colIndex
value 1 will definitely designate column "A", value 2 = "B", value 3 = "C", etc.
My problem is particularly when we double the letters.
Do you have any idea on how I could achieve this in the simplest possible form?
Thanks! =)
This function will do it for you.
It is in VB.NET but I trust you'll be able to port it to C# if need be.I have updated the answer with the C# version of the function.
VB.NET
C#
I tested this up to column index 1000 and it worked without fail. I hope you find it useful.
Here is this. Translate a column index into an Excel Column Name
Shouldn't be to hard to make it recursive and give you exactly what you need. I hope this helps.