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

前端 未结 30 2080
鱼传尺愫
鱼传尺愫 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 00:56

    JavaScript Solution

    /**
     * Calculate the column letter abbreviation from a 1 based index
     * @param {Number} value
     * @returns {string}
     */
    getColumnFromIndex = function (value) {
        var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
        var remainder, result = "";
        do {
            remainder = value % 26;
            result = base[(remainder || 26) - 1] + result;
            value = Math.floor(value / 26);
        } while (value > 0);
        return result;
    };
    

提交回复
热议问题