zoukankan      html  css  js  c++  java
  • C#中 Excel列字母与数字的相互转换

     1 //Excel列字母转数字
     2 public static int ToIndex(string columnName)
     3 {
     4     if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
     5 
     6     int index = 0;
     7     char[] chars = columnName.ToUpper().ToCharArray();
     8     for (int i = 0; i < chars.Length; i++)
     9     {
    10         index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
    11     }
    12     return index - 1;
    13 }
    14 
    15 //Excel数字转列字母
    16 public static string ToName(int index)
    17 {
    18     if (index < 0) { throw new Exception("invalid parameter"); }
    19 
    20     List<string> chars = new List<string>();
    21     do
    22     {
    23         if (chars.Count > 0) index--;
    24         chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
    25         index = (int)((index - index % 26) / 26);
    26     } while (index > 0);
    27 
    28     return String.Join(string.Empty, chars.ToArray());
    29 }
  • 相关阅读:
    VS Code 调试报错
    Nginx反向代理设置
    Nginx 的配置文件
    Nginx 的常用的命令
    CentOS7安装Nginx
    Docker配置
    Centos7 安装MySQL 5.7
    限制Redis使用的最大内存
    C#操作Redis
    Font Awesome 字体图标
  • 原文地址:https://www.cnblogs.com/DerekDeng/p/13646605.html
Copyright © 2011-2022 走看看