• c# excel数字索引与字母索引转换


     1 public static int NameToIndex(string columnName)
     2 {
     3   if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("参数非法!"); }
     5   int index = 0;
     6   char[] chars = columnName.ToUpper().ToCharArray();
     7   for (int i = 0; i < chars.Length; i++)
     8   {
     9     index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
    10   }
    11   return index;
    12 }
    13 
    14 /// <summary>
    15 /// 1、2、3 转换A、B、C
    18 /// <param name="index"></param>
    19 /// <returns></returns>
    20 public static string IndexToName(int index)
    21 {
    22   if (index < 0) { throw new Exception("参数非法!"); }
    24   List<string> chars = new List<string>();
    25   do
    26   {
    27     if (chars.Count > 0) index--;
    28     string tempchar = "";
    29     if (chars.Count == 0)
    30     {
    31       tempchar = ((char)(index % 26 + (int)'A' - 1)).ToString();
    32     }
    33     else {
    34       tempchar = ((char)(index % 26 + (int)'A')).ToString();
    35     }
    36     chars.Insert(0, tempchar);
    37     index = (int)((index - index % 26) / 26);
    38   } while (index > 0);
    39   return String.Join(string.Empty, chars.ToArray());
    40 }
  • 相关阅读:
    [No0000161]IDEA初步接触
    [No0000171]wpf 类层次结构Class Hierarchy
    [No0000160]常用C# 正则表达式大全
    [No000015D]【李笑来 笔记整理】个人商业模式升级
    thinkphp 系统变量
    thinkphp不读取.env文件的键对值
    thinkphp 模板变量输出替换和赋值
    thinkphp 视图view
    thinkphp 响应对象response
    Thinkphp 请求和响应
  • 原文地址:https://www.cnblogs.com/shijunfeng/p/6739133.html
Copyright © 2020-2023  润新知