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爱好者,正宗测试实践者。
  • 相关阅读:
    mysql 函数 存储过程 事件(event) job 模板
    protobuf 无proto 解码 decode 语言 java python
    mitmproxy fiddler 抓包 填坑
    android adb 常用命令
    android机器人 模拟 踩坑过程
    RabbitMQ添加新用户并支持远程访问
    Windows下RabbitMQ安装及配置
    Java mybatis mysql 常用数据类型对应关系
    easyExcel 踩坑
    linux防火墙查看状态firewall、iptable
  • 原文地址:https://www.cnblogs.com/herbert/p/1824962.html
Copyright © 2011-2022 走看看