question

GaniTPT avatar image
0 Votes"
GaniTPT asked ·

How do i convert column name from integer value

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"

dotnet-csharp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anyone can help us to proceed further...?

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

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;
     }
 }
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Really sorry. i didn't noticed thant.

How i could match the above scenario in my requirement.

for (int i = DigitStartY; i <= DigitEndY; i++)
{
**char FindCol = (char)(i + 64); ==>
ColumnName = FindCol.ToString(); ??????????????????????

}

0 Votes 0 ·

I would not use your current code. Get index of column would be

 var column = "AF";
 if (dictionary.ContainsValue(column))
 {
     var myKey = dictionary.FirstOrDefault(x => x.Value == column).Key;
     Debug.WriteLine(myKey);
 }
 else
 {
     Debug.WriteLine($"{column}");
 }

Get index

 List<string> resultList = Enumerable
     .Range(1, 100)
     .Select(index => index.ExcelColumnName()).ToList();
    
 var column = "AF";
 var columnValue = resultList.FirstOrDefault(item => item == column);


0 Votes 0 ·