Fastest function to generate Excel column letters in C#

前端 未结 21 1678
无人共我
无人共我 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:50

    The code I'm providing is NOT C# (instead is python) but the logic can be used for any language.

    Most of previous answers are correct. Here is one more way of converting column number to excel columns. solution is rather simple if we think about this as a base conversion. Simply, convert the column number to base 26 since there is 26 letters only. Here is how you can do this:

    steps:

    • set the column as a quotient

    • subtract one from quotient variable (from previous step) because we need to end up on ascii table with 97 being a.

    • divide by 26 and get the remainder.

    • add +97 to remainder and convert to char (since 97 is "a" in ASCII table)
    • quotient becomes the new quotient/ 26 (since we might go over 26 column)
    • continue to do this until quotient is greater than 0 and then return the result

    here is the code that does this :)

    def convert_num_to_column(column_num):
        result = ""
        quotient = column_num
        remainder = 0
        while (quotient >0):
            quotient = quotient -1
            remainder = quotient%26
            result = chr(int(remainder)+97)+result
            quotient = int(quotient/26)
        return result
    
    print("--",convert_num_to_column(1).upper())
    
    0 讨论(0)
  • 2020-11-29 02:51

    I can tell you that the fastest function will not be the prettiest function. Here it is:

    private string[] map = new string[]
        { 
            "A", "B", "C", "D", "E" .............
        };
    
    public string getColumn(int number)
    {
        return map[number];
    }
    
    0 讨论(0)
  • 2020-11-29 02:51

    The absolute FASTEST, would be capitalizing that the Excel spreadsheet only a fixed number of columns, so you would do a lookup table. Declare a constant string array of 256 entries, and prepopulate it with the strings from "A" to "IV". Then you simply do a straight index lookup.

    0 讨论(0)
  • 2020-11-29 02:51

    This is written in Java, but it's basically the same thing.

    Here's code to compute the label for the column, in upper-case, with a 0-based index:

    public static String findColChars(long index) {
        char[] ret = new char[64];
        for (int i = 0; i < ret.length; ++i) {
            int digit = ret.length - i - 1;
            long test = index - powerDown(i + 1);
            if (test < 0)
                break;
            ret[digit] = toChar(test / (long)(Math.pow(26, i)));
        }
        return new String(ret);
    }
    
    private static char toChar(long num) {
        return (char)((num % 26) + 65);
    }
    

    Here's code to compute 0-based index for the column from the upper-case label:

    public static long findColIndex(String col) {
        long index = 0;
        char[] chars = col.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            int cur = chars.length - i - 1;
            index += (chars[cur] - 65) * Math.pow(26, i);
        }
        return index + powerDown(chars.length);
    }
    
    private static long powerDown(int limit) {
        long acc = 0;
        while (limit > 1)
            acc += Math.pow(26, limit-- - 1);
        return acc;
    }
    
    0 讨论(0)
  • 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];
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-29 02:51

    barrowc's idea is much more convenient and fastest than any conversion function! i have converted his ideas to actual c# code that i use:

      var start = m_xlApp.Cells[nRow1_P, nCol1_P];
      var end = m_xlApp.Cells[nRow2_P, nCol2_P];
      // cast as Range to prevent binding errors
      m_arrRange = m_xlApp.get_Range(start as Range, end as Range);
      object[] values = (object[])m_arrRange.Value2;
    
    0 讨论(0)
提交回复
热议问题