zoukankan      html  css  js  c++  java
  • Excel列字母和数字的转换

    网上看的,收藏备用

    //用于excel表格中列号字转成数字,返回的列号索引从1开始
            public  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;
            }
    
     //用于将数字转成excel表格中列号字母,返回的列号索引从A开始,从A对应1开始
            public string ToName(int index)
            {
                if (index <= 0)
                    throw new Exception("invaild parameter");
                
                index--;
                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());
            }
    
    伪python爱好者,正宗测试实践者。
  • 相关阅读:
    RabbitMQ从入门到精通(一)
    MQ的架构作用
    Docker可视化管理工具
    Linux修改war包中文件
    Redis--各个数据类型最大存储量
    linux中直接修改jar包内配置文件
    脚本发布程序
    maven 安装到私服
    HTML基础 text-indent 把文字移出浏览器,隐藏起来
    HTML基础 td valign 设置文本靠上 居中 靠下
  • 原文地址:https://www.cnblogs.com/herbert/p/1824962.html
Copyright © 2011-2022 走看看