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

Possible duplicate:
How to convert a column number (e.g. 127) to an excel column (e.g. AA)

Ok, so I am writing a method that takes a 2d array as a parameter. I want to place this 2d array on an Excel worksheet, but I need to define the final cell. I can get the height easy enough:

var height = data.GetLength(1); //`data` is the name of my 2d array

It gives me an axis Y, but it's not easy for me to get an axis X. Obviously, I can get this number:

var width = data.GetLength(0);

This gives me the number 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 returns to A. I am sure there is an easy way to do this, but I have a complete mental block.

What would you do?

thank

+5
2

, ( 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;
}
+23

char "ToString()".

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void RunSnippet()
    {
        int myNumber = 65;
        string myLetter = ((char) myNumber).ToString();
        WL(myLetter);
    }

    #region Helper methods

    public static void Main()
    {
        try
        {
            RunSnippet();
        }
        catch (Exception e)
        {
            string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
            Console.WriteLine(error);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);   
    }

    private static void RL()
    {
        Console.ReadLine(); 
    }

    private static void Break() 
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}
+1

All Articles