Search This Blog

Thursday, April 4, 2013

C# Code Excel Get Column Name Using Column Number

We use row and column number to access the values in Ms Excel cells.

In program we use  row and col as number. but When we open Excel Column is represented as alphabets as A, B, C......AA, AB, AC....



Following is the code to convert column number into Column name.


 

private string GetExcelColumnName(int columnNumber)
{
    int dividend = columnNumber;
    string columnName = String.Empty;
    int modulo;

    while (dividend > 0)
    {
    modulo = (dividend - 1) % 26;
    columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
    dividend = (int)((dividend - modulo) / 26);
    }

    return columnName;
}

No comments:

Post a Comment