Fastest function to generate Excel column letters in C#

前端 未结 21 1783
无人共我
无人共我 2020-11-29 02:30

What is the fastest c# function that takes and int and returns a string containing a letter or letters for use in an Excel function? For example, 1 returns \"A\", 26 return

21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 02:52

    It is recursive. Fast, and right :

    class ToolSheet
    {
    
    
        //Not the prettyest but surely the fastest :
        static string[] ColName = new string[676];
    
    
        public ToolSheet()
        {
            ColName[0] = "A";
            for (int index = 1; index < 676; ++index) Recurse(index, index);
    
        }
    
        private int Recurse(int i, int index)
        {
            if (i < 1) return 0;
            ColName[index] = ((char)(65 + i % 26)).ToString() + ColName[index];
    
            return Recurse(i / 26, index);
        }
    
        public string GetColName(int i)
        {
            return ColName[i - 1];
        }
    
    
    
    }
    

提交回复
热议问题