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

前端 未结 30 2102
鱼传尺愫
鱼传尺愫 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:01

    Just throwing in a simple two-line C# implementation using recursion, because all the answers here seem far more complicated than necessary.

    /// 
    /// Gets the column letter(s) corresponding to the given column number.
    /// 
    /// The one-based column index. Must be greater than zero.
    /// The desired column letter, or an empty string if the column number was invalid.
    public static string GetColumnLetter(int column) {
        if (column < 1) return String.Empty;
        return GetColumnLetter((column - 1) / 26) + (char)('A' + (column - 1) % 26);
    }
    

提交回复
热议问题