让字母自增,就像A-Z,Z后面就是AA-AZ,然后BA-BZ...

       string GetNextChars(string letter)
        {
            string letterTemp = letter.Trim();
            int length = letterTemp.Length;

            int res = 0;
            for (int i = 0; i < length; i++)//先转成数字 A=1 Z=26 AZ=52
            {
                res = res * 26 + letterTemp[i] - 'A' + 1;
            }

            res++;

            string endCol = string.Empty;
            string endColSignal = string.Empty;
            int iCnt = (res / 26);
            if (res >= 26 && res % 26 == 0)
            {
                int icell = iCnt - 2;
                endCol = (icell < 0 ? string.Empty : ((char)('A' + icell)).ToString()) + "Z";
            }
            else
            {

                endColSignal = (iCnt == 0 ? "" : ((char)('A' + (iCnt - 1))).ToString());
                int icell = res - iCnt * 26 - 1;
                if (icell < 0)
                    icell = 0;
                endCol = endColSignal + ((char)('A' + icell)).ToString();
            }
            return endCol;
        }

你可能感兴趣的:(.NET(C#,WinForm),A-Z)