Get Excel-Style Column Names from Column Number

前端 未结 8 1565
慢半拍i
慢半拍i 2020-12-01 12:38

This is the code for providing the COLUMN name when the row and col ID is provided but when I give values like row = 1 and col = 104, it should return CZ<

相关标签:
8条回答
  • 2020-12-01 13:22

    EDIT: I feel I must admit, as pointed out by a few others—who never left me comments—that the previous version of my answer (which you accepted) had a bug that prevented it from properly handling column numbers greater than 702 (corresponding to Excel column 'ZZ'). So, in the interests of correctness, that's been fixed in the code below, which now contains a loop just like many of the other answers do.

    It's quite likely you never used the previous version with large enough column numbers to have encountered the issue. FWIW, the MS specs for the current version of Excel say it supports worksheets with up to 16,384 columns (Excel column 'XFD').

    LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    def excel_style(row, col):
        """ Convert given row and column number to an Excel-style cell name. """
        result = []
        while col:
            col, rem = divmod(col-1, 26)
            result[:0] = LETTERS[rem]
        return ''.join(result) + str(row)
    
    if __name__ == '__main__':
        addresses = [(1,  1), (1, 26),
                     (1, 27), (1, 52),
                     (1, 53), (1, 78),
                     (1, 79), (1, 104),
                     (1, 18253), (1, 18278),
                     (1, 702),  # -> 'ZZ1'
                     (1, 703),  # -> 'AAA1'
                     (1, 16384), # -> 'XFD1'
                     (1, 35277039)]
    
        print('({:3}, {:>10}) --> {}'.format('row', 'col', 'Excel'))
        print('==========================')
        for row, col in addresses:
            print('({:3}, {:10,}) --> {!r}'.format(row, col, excel_style(row, col)))
    

    Output:

    (row,       col) --> Excel
    ========================
    (  1,         1) --> 'A1'
    (  1,        26) --> 'Z1'
    (  1,        27) --> 'AA1'
    (  1,        52) --> 'AZ1'
    (  1,        53) --> 'BA1'
    (  1,        78) --> 'BZ1'
    (  1,        79) --> 'CA1'
    (  1,       104) --> 'CZ1'
    (  1,     18253) --> 'ZZA1'
    (  1,     18278) --> 'ZZZ1'
    (  1,       702) --> 'ZZ1'
    (  1,       703) --> 'AAA1'
    (  1,     16384) --> 'XFD1'
    (  1,  35277039) --> 'BYEBYE1'
    
    0 讨论(0)
  • 2020-12-01 13:23

    You have a couple of index issues:

    So to fix your problem, you need to make all your indices match:

    def colToExcel(col): # col is 1 based
        excelCol = str()
        div = col 
        while div:
            (div, mod) = divmod(div-1, 26) # will return (x, 0 .. 25)
            excelCol = chr(mod + 65) + excelCol
    
        return excelCol
    
    print colToExcel(1) # => A
    print colToExcel(26) # => Z
    print colToExcel(27) # => AA
    print colToExcel(104) # => CZ
    print colToExcel(26**3+26**2+26) # => ZZZ
    
    0 讨论(0)
提交回复
热议问题