Fastest function to generate Excel column letters in C#

前端 未结 21 1738
无人共我
无人共我 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 03:04

    My solution:

    static class ExcelHeaderHelper
    {
        public static string[] GetHeaderLetters(uint max)
        {
            var result = new List<string>();
            int i = 0;
            var columnPrefix = new Queue<string>();
            string prefix = null;
            int prevRoundNo = 0;
            uint maxPrefix = max / 26;
    
            while (i < max)
            {
                int roundNo = i / 26;
                if (prevRoundNo < roundNo)
                {
                    prefix = columnPrefix.Dequeue();
                    prevRoundNo = roundNo;
                }
                string item = prefix + ((char)(65 + (i % 26))).ToString(CultureInfo.InvariantCulture);
                if (i <= maxPrefix)
                {
                    columnPrefix.Enqueue(item);
                }
                result.Add(item);
                i++;
            }
            return result.ToArray();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 03:09

    I currently use this, with Excel 2007

    public static string ExcelColumnFromNumber(int column)
            {
                string columnString = "";
                decimal columnNumber = column;
                while (columnNumber > 0)
                {
                    decimal currentLetterNumber = (columnNumber - 1) % 26;
                    char currentLetter = (char)(currentLetterNumber + 65);
                    columnString = currentLetter + columnString;
                    columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
                }
                return columnString;
            }
    

    and

    public static int NumberFromExcelColumn(string column)
            {
                int retVal = 0;
                string col = column.ToUpper();
                for (int iChar = col.Length - 1; iChar >= 0; iChar--)
                {
                    char colPiece = col[iChar];
                    int colNum = colPiece - 64;
                    retVal = retVal + colNum * (int)Math.Pow(26, col.Length - (iChar + 1));
                }
                return retVal;
            }
    

    As mentioned in other posts, the results can be cached.

    0 讨论(0)
  • 2020-11-29 03:10

    Here's my version: This does not have any limitation as such 2-letter or 3-letter. Simply pass-in the required number (starting with 0) Will return the Excel Column Header like Alphabet sequence for passed-in number:

    private string GenerateSequence(int num)
    {
        string str = "";
        char achar;
        int mod;
        while (true)
        {
            mod = (num % 26) + 65;
            num = (int)(num / 26);
            achar = (char)mod;
            str = achar + str;
            if (num > 0) num--;
            else if (num == 0) break;
        }
        return str;
    }
    

    I did not tested this for performance, if someone can do that will great for others. (Sorry for being lazy) :)

    Cheers!

    0 讨论(0)
提交回复
热议问题