public static int ToIndex(string columnName)
{
    if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
 
    int index = 0;
    char[] chars = columnName.ToUpper().ToCharArray();
    for (int i = 0; i < chars.Length; i++)
    {
        index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
    }
    return index - 1;
}
 
 
public static string ToName(int index)
{
    if (index < 0) { throw new Exception("invalid parameter"); }
 
    List<string> chars = new List<string>();
    do
    {
        if (chars.Count > 0) index--;
        chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
        index = (int)((index - index % 26) / 26);
    } while (index > 0);
 
    return String.Join(string.Empty, chars.ToArray());
}

转自:https://www.cnblogs.com/lhlong/p/7920408.html

相关文章:

  • 2021-08-28
  • 2022-12-23
  • 2021-12-10
  • 2022-01-29
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-10
  • 2022-01-07
  • 2021-09-17
  • 2021-12-12
  • 2021-12-10
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案