zoukankan      html  css  js  c++  java
  • c# excel的列转换成数字

    转载:http://www.cnblogs.com/msgarden/p/5129927.html

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.Regularexpression_rs;
     
    namespace Hooogle
    {
        public static class ExcelConvert
        {
            #region - 由数字转换为Excel中的列字母 -
            
            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());
            }
            #endregion
        }
    }
    

      

  • 相关阅读:
    计算机网络基础
    ansible
    CDH集群日常
    漏洞挖掘学习
    JDWP
    开源安全项目调研
    SMB漏洞汇总
    Windows账户LM/NTLM
    SMB知识汇总
    Memcache未授权漏洞
  • 原文地址:https://www.cnblogs.com/lhlong/p/7171890.html
Copyright © 2011-2022 走看看