How do i convert column name from integer value

BeUnique 2,112 Reputation points
2021-03-15T17:15:22.013+00:00

i used below code. If i used my DigitStartY,DigitEndY value < 26 then it's getting proper column name value.

if i used more than 26, then i am getting error and not able to get the column name value.

Here i =28 then getting FindCol "92 \"

for (int i = DigitStartY; i <= DigitEndY; i++)
{
char FindCol = (char)(i + 64); ==> will it work if AA,,,ZZ...????
ColumnName = FindCol.ToString();

}

char FindCol = (char)(i + 64); ==> I am getting "92 \" ==> Suppose to come column name as "AB"

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,627 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-03-16T02:08:22.073+00:00

    Please try if this method can meet your needs:

            private static string GetColumnName(int colNum)  
            {  
                string columnName = String.Empty;  
                int modulo;  
                while (colNum > 0)  
                {  
                    modulo = (colNum - 1) % 26;  
                    columnName = Convert.ToChar(65 + modulo).ToString() + columnName;  
                    colNum = (int)((colNum - modulo) / 26);  
                }  
                return columnName;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2021-03-16T01:10:11.65+00:00

    I'm curious about my reply in you other post on this subject as to why it didn't fit your needs?

    Here is another version and the 100 is whatever value you want.

    List<string> resultList = Enumerable
        .Range(1, 100)
        .Select(index => index.ExcelColumnName()).ToList();
    
    Dictionary<int, string> dictionary = Enumerable
        .Range(1, 100)
        .Select((index) => new {Index = index,Name = index.ExcelColumnName()})
        .ToList()
        .ToDictionary(kvp => kvp.Index, kvp => kvp.Name);
    

    Extension

    public static class ExtensionMethods
    {
        public static string ExcelColumnName(this int index)
        {
            var chars = new[]
            {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            };
    
            index -= 1;
            string columnName;
            var quotient = index / 26;
    
            if (quotient > 0)
            {
                columnName = ExcelColumnName(quotient) + chars[index % 26];
            }
            else
            {
                columnName = chars[index % 26].ToString();
            }
            return columnName;
        }
    }