Translate a column index into an Excel Column Name

前端 未结 15 2278
花落未央
花落未央 2020-11-27 20:41

Given a columns\' index, how can you get an Excel column name?

The problem is trickier than it sounds because it\'s not just base-26. The columns

15条回答
  •  遥遥无期
    2020-11-27 20:53

    Here's Joel's awesome code modified to work with zero-based column indexes and without the char array.

     Public Shared Function GetExcelColumn(ByVal index As Integer) As String
    
            Dim quotient As Integer = index \ 26 ''//Truncate 
            If quotient > 0 Then
                Return GetExcelColumn(quotient - 1) & Chr((index Mod 26) + 64).ToString
    
            Else
                Return Chr(index + 64).ToString
    
            End If
    
        End Function
    

提交回复
热议问题