2011-06-23 91 views
12

我正在将我的数据导出到Excel Using Open XML。现在我想增加字母表像columns 'A1' to 'B1',...'Z1', 'AA1'.c中字母的递增#

我已经分配'A1'到变量,我想增加字母'B1'。

请提供任何方法/代码,通过它可以增加字母'A1'到'B1'..'Z1','AA1'。

+0

您是否自己找到任何代码? – BoltClock

+0

这应该是一个非常简单的数据结构,并且您已经知道您想要执行的功能... – crashmstr

+0

有点相关的问题[here](https://stackoverflow.com/q/2208688/465053)。 – RBT

回答

28

这是可以做到:

char c1 = 'A'; 
c1++; // c1 is 'B' now 

,你可以添加编号为字符串,即使是级联的字符可以产生同样的方式:

伪代码:

If Reached_Z Then Add_Another_A 
+2

不错!完美的作品,读得很好,而且不复杂。 – Brian

4

本示例使用能够从AZZ的迭代器。

public static IEnumerable<string> GetColumns() 
{ 
    string s = null; 
    for (char c2 = 'A'; c2 <= 'Z' + 1; c2++) 
    { 
     for (char c = 'A'; c <= 'Z'; c++) 
     { 
     yield return s + c; 
     } 
     s = c2.ToString(); 
    } 
} 

此示例开始于A1并通过AA1

string currentCell = "A1"; 
int currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value); 
string currentCol = Regex.Match(currentCell, @"[A-Z]+").Value; 
foreach (string column in GetColumns().Where (c => c >= currentCol && currentCol <= "AA")) 
{ 
    Console.WriteLine (column + currentRow); 
} 

此示例开始于C5和枚举通过接下来的26列去。

int columnsToAdd = 26; 
currentCell = "C5"; 
currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value); 
currentCol = Regex.Match(currentCell, @"[A-Z]+").Value; 
foreach (string column in GetColumns().Where (c => c >= currentCol)) 
{ 
    if (columnsToAdd--) == 0) 
     break; 
    Console.WriteLine (column + currentRow); 
} 
1

我觉得这些功能做你想要什么:

public static string IncrementXLColumn(string Address) 
    { 
     var parts = System.Text.RegularExpressions.Regex.Matches(Address, @"([A-Z]+)|(\d+)"); 
     if (parts.Count != 2) return null; 
     return incCol(parts[0].Value) + parts[1].Value; 
    } 

    private static string incCol(string col) 
    { 
     if (col == "") return "A"; 
     string fPart = col.Substring(0, col.Length - 1); 
     char lChar = col[col.Length - 1]; 
     if (lChar == 'Z') return incCol(fPart) + "A"; 
     return fPart + ++lChar; 
    } 

此功能将采取A1的串B1,Z1至AA1等应与应对ZZ 1至AAA1以及

0

这种方法会给你下一个列:

private static Regex ALL_Z_REGEX = new Regex("^[zZ]+$"); 

    static string GetNextColumn(string currentColumn) 
    { 
     // AZ would become BA 
     char lastPosition = currentColumn[currentColumn.Length - 1]; 

     if (ALL_Z_REGEX.IsMatch(currentColumn)) 
     { 
      string result = String.Empty; 
      for (int i = 0; i < currentColumn.Length; i++) 
       result += "A"; 
      return result + "A"; 
     } 
     else if (lastPosition == 'Z') 
      return GetNextColumn(currentColumn.Remove(currentColumn.Length - 1, 1)) + "A"; 
     else 
      return currentColumn.Remove(currentColumn.Length - 1, 1) + (++lastPosition).ToString(); 
    } 
-1

你可以使用字符串生成器来实现这一点。

  int length = value.Length; 
     var lastString = value[length - 1]; 
     if (lastString == 'Z') 
     { 
      if ((length - 2) >= 0) 
       ++value[length - 2]; 
      else 
       value.Append('A'); 

      value.Replace("Z", "A"); 
     } 
     else 
      ++value[length - 1];