Convert a number to a letter in C# for use in Microsoft Excel

algorithm, c#, excel

Solution

Here's a version that also handles two-letter columns (after column Z):

static string GetColumnName(int index)
{
    const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var value = "";

    if (index >= letters.Length)
        value += letters[index / letters.Length - 1];

    value += letters[index % letters.Length];

    return value;
}

Problem

Possible Duplicate: How to convert a column number (eg. 127) into an excel column (eg. AA) Ok so I am writing a method which accepts a 2d array as a parameter. I want to put this 2d array onto an Excel worksheet but I need to work out the final cell location. I can get the height easily enough like so: ``` var height = data.GetLength(1); //`data` is the name of my 2d array ``` This gives me my `Y` axis but it's not so easy to get my `X` axis. Obviously I can get the number like so: ``` var width = data.GetLength(0); ``` This gives me a number, which I want to convert to a letter. So, for example, 0 is A, 1 is B and so on until we get to 26 which goes back to A again. I am sure that there is a simple way to do this but I have a total mental block. What would you do? Thanks

Original source

Related problems