18

I was wondering what is the best way to convert excel sheet column names into numbers.

I'm working with Excel Package, a good library to handle .xlsx documents. This library unfortunately doesn't have this functionality included.

OBS: The first column, A, corresponds to number 1 in this library.

Victor Rodrigues
  • 11,353
  • 23
  • 75
  • 107

6 Answers6

65

This function should work for an arbitrary length column name.

public static int GetColumnNumber(string name)
{
    int number = 0;
    int pow = 1;
    for (int i = name.Length - 1; i >= 0; i--)
    {
        number += (name[i] - 'A' + 1) * pow;
        pow *= 26;
    }

    return number;
}
Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • 4
    **Note** that this only works as expected, when the given **column name** (`name`) **is uppercase**. (Replace `name[i]` with `name.ToUpper()[i]` to fix this.) – Marcus Mangelsdorf Oct 18 '16 at 14:01
  • You can make it run faster *and* remove the need for a `pow` variable by using [Horner's method](https://en.wikipedia.org/wiki/Horner%27s_method) for general polynomial evaluation. This also means you get to use a normal `i=0; i – Ethan Maness May 31 '23 at 23:05
17

I had to deal with this a few months ago. The inverse - column index to column name - is fun, too, and becomes really messy if you try to solve it with a zero based index not recognizing that this complicates things. It could be so simple if it would be a normal polyadic numeral system ...

Here is a simplified version of my solution as a extension method without error handling and all that stuff.

public static Int32 ToOneBasedIndex(this String name)
{
    return name.ToUpper().
       Aggregate(0, (column, letter) => 26 * column + letter - 'A' + 1);
}
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
3

I've been working with this for a while now and found this to work really good for columns that go beyond A-Z, or even beyond AA-ZZ... It's accomplished by breaking apart each character in the string and recursively calling itself to derive the DEC value of the ASCII character (less 64), then multiplying it by 26^n. A return value of long was used to overcome a potential limitation when n > 4.

    public long columnNumber(String columnName)
    {
        char[] chars = columnName.ToUpper().ToCharArray();

        return (long)(Math.Pow(26, chars.Count() - 1)) * 
            (System.Convert.ToInt32(chars[0]) - 64) + 
            ((chars.Count() > 2) ? columnNumber(columnName.Substring(1, columnName.Length - 1)) : 
            ((chars.Count() == 2) ? (System.Convert.ToInt32(chars[chars.Count() - 1]) - 64) : 0));
    }

Also, if you'd like to get the inverse (i.e. pass in the columnNumber and get the columnName, here's some code that works for that.

    public String columnName(long columnNumber)
    {
        StringBuilder retVal = new StringBuilder();
        int x = 0;

        for (int n = (int)(Math.Log(25*(columnNumber + 1))/Math.Log(26)) - 1; n >= 0; n--)
        {
            x = (int)((Math.Pow(26,(n + 1)) - 1) / 25 - 1);
            if (columnNumber > x)
                retVal.Append(System.Convert.ToChar((int)(((columnNumber - x - 1) / Math.Pow(26, n)) % 26 + 65)));
        }

        return retVal.ToString();
    }
1

Source code:

namespace XLS
{
/// <summary>
/// Represents a single cell in a excell sheet
/// </summary>
public struct Cell
{
    private long row;
    private long column;
    private string columnAddress;
    private string address;
    private bool dataChange;

    /// <summary>
    /// Initializes a new instance of the XLS.Cell 
    /// class with the specified row and column of excel worksheet
    /// </summary>
    /// <param name="row">The row index of a cell</param>
    /// <param name="column">The column index of a cell</param>
    public Cell(long row, long column)
    {
        this.row = row;
        this.column = column;
        dataChange = true;
        address = string.Empty;
        columnAddress = string.Empty;
    }

    /// <summary>
    /// Initializes a new instance of the XLS.Cell
    /// class with the specified address of excel worksheet
    /// </summary>
    /// <param name="address">The adress of a cell</param>
    public Cell(string address)
    {
        this.address = address;
        dataChange = false;
        row = GetRow(address);
        columnAddress = GetColumnAddress(address);
        column = GetColumn(columnAddress);
    }

    /// <summary>
    /// Gets or sets the row of this XLS.Cell
    /// </summary>
    public long Row
    {
        get { return row <= 0 ? 1 : row; }
        set { row = value; dataChange = true; }
    }

    /// <summary>
    /// Gets or sets the column of this XLS.Cell
    /// </summary>
    public long Column
    {
        get { return column <= 0 ? 1 : column; }
        set { column = value; dataChange = true; }
    }

    /// <summary>
    /// Gets or sets the address of this XLS.Cell
    /// </summary>
    public string Address
    {
        get { return dataChange ? ToAddress() : address; }
        set
        {
            address = value;
            row = GetRow(address);
            column = GetColumn(address);
        }
    }

    /// <summary>
    /// Gets the column address of this XLS.Cell
    /// </summary>
    public string ColumnAddress
    {
        get { return GetColumnAddress(Address); }
        private set { columnAddress = value; }
    }

    #region Private Methods

    private static long GetRow(string address)
    {
        return long.Parse(address.Substring(GetStartIndex(address)));
    }

    private static string GetColumnAddress(string address)
    {
        return address.Substring(0, GetStartIndex(address)).ToUpperInvariant();
    }

    private static long GetColumn(string columnAddress)
    {
        char[] characters = columnAddress.ToCharArray();
        int sum = 0;
        for (int i = 0; i < characters.Length; i++)
        {
            sum *= 26;
            sum += (characters[i] - 'A' + 1);
        }

        return (long)sum;
    }

    private static int GetStartIndex(string address)
    {
        return address.IndexOfAny("123456789".ToCharArray());
    }

    private string ToAddress()
    {
        string indexToString = string.Empty;

        if (Column > 26)
        {
            indexToString = ((char)(65 + (int)((Column - 1) / 26) - 1)).ToString();
        }

        indexToString += (char)(65 + ((Column - 1) % 26));

        dataChange = false;
        return indexToString + Row;
    }

    #endregion
}

}

0

O24 has a column number and you want a name:

=LEFT(RIGHT(ADDRESS(1,O24),LEN(ADDRESS(1,O24))-1),FIND("$",RIGHT((ADDRESS(1,O24)),LEN(ADDRESS(1,O24))-1))-1)

O37 has a column name and you want a number:

=COLUMN(INDIRECT(O37&1))

George K.
  • 1
  • 1
0
public static string GetColumnName(int index)
{
    const string letters = "ZABCDEFGHIJKLMNOPQRSTUVWXY";

    int NextPos = (index / 26);
    int LastPos = (index % 26);
    if (LastPos == 0) NextPos--;

    if (index > 26)
        return GetColumnName(NextPos) + letters[LastPos];
    else
        return letters[LastPos] + "";
}