请问#怎么生成自增流水号,A0001-A9999.......→Z0001-Z9999→ZA001-ZA999.....→ZZ999

请问#怎么生成自增流水号,A0001-A9999.......→Z0001-Z9999→ZA001-ZA999.....→ZZ999

问题描述:

请问#怎么生成自增流水号,规则→ A0001-A9999.......→Z0001-Z9999→ZA001-ZA999.....→ZZ999

问题解决的话,请点下采纳

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Q1095767
{
    class Program
    {
        static string getnext(string code)
        {
            string num = Regex.Match(code, "\\d+").Value;
            string lett = Regex.Match(code, "[A-Z]+").Value;
            string lett1 = "";
            int ord = 0;
            foreach (char ch in lett)
            {
                ord = ord + (ch - 'A' + 1);
            }
            ord++;
            while (true)
            {
                if (ord >= 26)
                {
                    lett1 += "Z";
                    ord -= 26;
                }
                else
                {
                    if (ord != 0)
                        lett1 += ((char)(ord + 'A' - 1)).ToString();
                    break;
                }
            }
            string num1 = (int.Parse(num) + 1).ToString();
            if (num.TrimStart('0').Length == num1.Length)
                return lett + num1.PadLeft(num.Length, '0');
            else
            {
                if (num.All(x => x == '9'))
                    return lett1.PadRight(4, '0') + "1";
                else
                    return lett + num1.PadLeft(5 - lett.Length, '0');
            }
        }
        static void Main(string[] args)
        {
            string[] tests = { "A0001", "A9999", "Y9999", "Z9999", "ZA999", "ZE888", "ZZ999" };
            foreach (var s in tests)
            {
                Console.Write(s + " ");
                string s1 = s;
                for (int i = 1; i < 12; i++) { s1 = getnext(s1); Console.Write(s1 + " "); }
                Console.WriteLine();
            }
        }
    }
}

A0001 A0002 A0003 A0004 A0005 A0006 A0007 A0008 A0009 A0010 A0011 A0012
A9999 B0001 B0002 B0003 B0004 B0005 B0006 B0007 B0008 B0009 B0010 B0011
Y9999 Z0001 Z0002 Z0003 Z0004 Z0005 Z0006 Z0007 Z0008 Z0009 Z0010 Z0011
Z9999 ZA001 ZA002 ZA003 ZA004 ZA005 ZA006 ZA007 ZA008 ZA009 ZA010 ZA011
ZA999 ZB001 ZB002 ZB003 ZB004 ZB005 ZB006 ZB007 ZB008 ZB009 ZB010 ZB011
ZE888 ZE889 ZE890 ZE891 ZE892 ZE893 ZE894 ZE895 ZE896 ZE897 ZE898 ZE899
ZZ999 ZZA01 ZZA02 ZZA03 ZZA04 ZZA05 ZZA06 ZZA07 ZZA08 ZZA09 ZZA10 ZZA11
Press any key to continue . . .