Fastest function to generate Excel column letters in C#

前端 未结 21 1789
无人共我
无人共我 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条回答
  •  一个人的身影
    2020-11-29 02:51

    sorry there was a shift. corrected.

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

提交回复
热议问题