Home > Development > Excel Column Name Calculator

Excel Column Name Calculator

I need to calculate the column name of a cell in Excel so here is a class I am using to do so.

public class ExcelColumnCalculator
        private int index;
        private readonly char[] 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' };

        public ExcelColumnCalculator(int index)
            this.index = index;

        public string CalculateColumnName()
            return ColumnName(index);

        private string ColumnName(int position)
            int quotient = position / 26;
            if (quotient > 0)
                return ColumnName(quotient) + chars[position % 26];
            return chars[position % 26].ToString();

        private void AdjustIndexToZeroBased()
            index -= 1; //adjust so it matches 0-indexed array rather than 1-indexed column    

        public int Index
            get { return index; }



  1. Matt
    June 23, 2010 at 2:06 pm

    Doesn’t work… 26 returns BA when it should return AA. Your quotient needs decrementing to cope with the zero indexing. Good solution though!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: