问题描述
我希望能够以A,B,C等命名单元格实例来实例化我的单元格
类像在Excel电子表格中一样。
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.
我有我的单元格
类如下:
public class Cell {
public Cell(Range nativeCell) {
NativeCell = nativeCell;
}
public Range NativeCell { get; private set; }
}
而我的 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; }
}
我需要根据字母字母生成一个名称,一旦遇到最后一个字母Z,就会三倍。该算法必须生成我将连接到将导致此命名策略(例如Excel)的 rowIndex
值的字母。
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.
这些字母将是:
A, B, C, D...Z, AA, AB, AC...AZ, BA, BB, BC...BZ, CA...XAA, XAB, XAC...
虽然我们清楚地知道 colIndex
value 1将一定会指定列A,值2 =B,值3 =C等等。
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?
谢谢! =)
推荐答案
这是这个。
将列索引转换为Excel列名称
不应该很难让它递归,而是给你所需要的。我希望这有帮助。
Shouldn't be to hard to make it recursive and give you exactly what you need. I hope this helps.
这篇关于如何从其索引中实现列自身命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!