Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Base26 Number Encoding/Generate Excel Columns

0.00/5 (No votes)
2 May 2011CPOL 7.5K  
Base26 Number Encoding/Generate Excel Columns

Ever needed to convert an Excel Column Letter to a number or vice versa?  Here’s a couple of extension methods to do it:

C#
1: public static string ToLetterEncoded(this Int32 numberToEncode)
2: {
3:     if (numberToEncode <= 0) throw new ArgumentOutOfRangeException(
           "numberToEncode", numberToEncode, "Value must be greater than zero");
4:     numberToEncode--;
5:     if (numberToEncode >= 0 && numberToEncode < 26)
6:         return ((char)('A' + numberToEncode)).ToString();
7:     else
8:         return ToLetterEncoded(numberToEncode / 26) +
               ToLetterEncoded(numberToEncode % 26 + 1);
9: }
C#
  1: public static Int32 FromLetterEncodedInt32(this string letterEncodedNumber)
  2: {
  3:     if (letterEncodedNumber.IsNullOrWhiteSpace()) throw new ArgumentNullException(
             "letterEncodedNumber");
         //smallest column first
  4:     char[] letters =
             letterEncodedNumber.ToUpperInvariant().ToCharArray().Reverse().ToArray();
  5:     if (letters.Where(c => !char.IsLetter(c)).Any()) throw new ArgumentOutOfRangeException(
             "letterEncodedNumber",
             "Encoded Number must only contain the letters A-Z");
  6:     int[] numbers = letters.Select(c => (((int)c - 'A') + 1)).ToArray();
  7:     Int32 columnNumber = 0;
  8:     for (int i = 0; i < letters.Length; i++)
  9:     {
 10:         columnNumber += (Int32)Math.Pow(26, i) * numbers[i];
 11:     }
 12:     return columnNumber;
13: }

I’ve used Int32 to represent the type of integer that you’re en(de)coding, for easy find ‘n’ replace.

These will only work for positive integer (1 and over).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)