编程语言
首页 > 编程语言> > C# Excel列名与Int互转

C# Excel列名与Int互转

作者:互联网

       static int GetExcelColIndex(string x)
        {
            //string转int,如A转成int=0
            var result = 0;
            var arr= x.ToCharArray();
            for (int i = x.Length-1; i>=0; i--)
            {
                result += (arr[i]-'A'+1)*(int)Math.Pow(26,x.Length-i-1);
            } 
            return result-1; 
        }

        private static string GetExcelCol(int colIndex)
        { 
            //int转string,如0转换成string=A
            var major = colIndex / 26;
            var minor = colIndex % 26;
            var last = ((char)(minor + 'A')).ToString();
            if (major > 0)
                return GetExcelCol(major - 1) + last;
            return last;
        }

 

标签:colIndex,string,C#,26,var,Int,int,result,互转
来源: https://www.cnblogs.com/lizhijian/p/13711981.html