如何从索引实现列自我命名?

我希望能够实例化我的Cell类,同时用“A”,“B”,“C”等命名单元实例,就像在Excel电子表格中一样。

我有我的Cell类如下所示:

 public class Cell { public Cell(Range nativeCell) { NativeCell = nativeCell; } public Range NativeCell { get; private set; } } 

和我的Sheet类:

 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”,我需要根据字母字母来生成一个名字,然后将它们翻一番和三倍。 该algorithm将不得不生成字母,我将连接rowIndex值,这将导致这种命名策略,如Excel。

这些信件将是:

 A, B, C, D...Z, AA, AB, AC...AZ, BA, BB, BC...BZ, CA...XAA, XAB, XAC... 

虽然我们清楚地知道colIndex值1将明确指定列“A”,值2 =“B”,值3“C”等。

我们的问题特别是当我们把字母加倍时。

你有什么想法,我怎样才能以最简单的forms实现这一点?

谢谢! =)

这是这个 将列索引转换为Excel列名称

不应该难以recursion,并给你正是你所需要的。 我希望这有帮助。

这个function会为你做。 这是在VB.NET,但我相信你可以将它移植到C#,如果需要的话。
我已经用C#版本的函数更新了答案。

VB.NET

 ''' <summary>Returns the Excel-style name of the column from the column index.</summary> ''' <param name="colIndex">The column index.</param> Function GetColumnName(ByVal colIndex As Integer) As String If colIndex < 1 Then Throw New ArgumentException("Column number must be greater or equal to 1.") Dim result As New List(Of String) 'letter codes start at Chr(65)' Do While colIndex > 0 'reduce the column number by 1 else the 26th column (Z) will become 0 (@) ' 'add 65 to the result and find the Chr() value. ' 'insert the character at position 0 of the character list ' 'integer divide by 26 to remove the column from the stack and repeat till ' 'there are no columns in the stack. ' result.Insert(0, Chr(65 + CInt((colIndex - 1) Mod 26))) colIndex = (colIndex - 1) \ 26 Loop Return String.Join("", result.ToArray) End Function 

C#

 /// <summary>Returns the Excel-style name of the column from the column index.</summary> /// <param name="colIndex">The column index.</param> static string GetColumnName(int colIndex) { if (colIndex < 1) throw new ArgumentException("Column number must be greater or equal to 1."); var result = new List<char>(); //letter codes start at Chr(65)' while (colIndex > 0) { //reduce the column number by 1 else the 26th column (Z) will become 0 (@) //add 65 to the result and find the Chr() value. //insert the character at position 0 of the char list //integer divide the column index by 26 to remove the last calculated column //from the stack and repeat till there are no columns in the stack. result.Insert(0, Microsoft.VisualBasic.Strings.Chr(65 + Convert.ToInt32((colIndex - 1) % 26))); colIndex = (int)((colIndex-1)/ 26); } return new string(result.ToArray()); } 

我testing了这个列索引1000,它的工作没有失败。 希望对你有帮助。