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

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

    Sorry, this is Python instead of C#, but at least the results are correct:

    def ColIdxToXlName(idx):
        if idx < 1:
            raise ValueError("Index is too small")
        result = ""
        while True:
            if idx > 26:
                idx, r = divmod(idx - 1, 26)
                result = chr(r + ord('A')) + result
            else:
                return chr(idx + ord('A') - 1) + result
    
    
    for i in xrange(1, 1024):
        print "%4d : %s" % (i, ColIdxToXlName(i))
    

提交回复
热议问题