How to convert a column number (e.g. 127) into an Excel column (e.g. AA)

前端 未结 30 2076
鱼传尺愫
鱼传尺愫 2020-11-22 00:35

How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.

Excel 2007 has a possible range o

30条回答
  •  旧巷少年郎
    2020-11-22 01:06

    A little late to the game, but here's the code I use (in C#):

    private static readonly string _Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static int ColumnNameParse(string value)
    {
        // assumes value.Length is [1,3]
        // assumes value is uppercase
        var digits = value.PadLeft(3).Select(x => _Alphabet.IndexOf(x));
        return digits.Aggregate(0, (current, index) => (current * 26) + (index + 1));
    }
    

提交回复
热议问题