The columns in Excel are called A, B, C, AA, AB, AC, ...
Here's a small snippet calculating the name based on a 26-digit system:
private static String GetExcelColumnLetters(Int32 column, Boolean indexIsZeroBased)
{
if (indexIsZeroBased == false)
{
column--;
}
Int32 firstLetter = column / 26;
Int32 secondLetter = column % 26;
String columName = String.Empty;
if (firstLetter > 25)
{
columName = GetExcelColumnLetters(firstLetter, true);
}
else if (firstLetter > 0)
{
columName = Convert.ToChar(firstLetter + 64).ToString();
}
columName += Convert.ToChar(secondLetter + 65).ToString();
return (columName);
}
private static void TestGetExcelColumnLetters(Int32 column)
{
String columName;
columName = GetExcelColumnLetters(column, false);
Console.WriteLine(columName);
columName = GetExcelColumnLetters(column - 1, true);
Console.WriteLine(columName);
}
static void Main()
{
TestGetExcelColumnLetters(1);
Console.WriteLine();
TestGetExcelColumnLetters(5);
Console.WriteLine();
TestGetExcelColumnLetters(26);
TestGetExcelColumnLetters(27);
TestGetExcelColumnLetters(28);
Console.WriteLine();
TestGetExcelColumnLetters(52);
TestGetExcelColumnLetters(53);
Console.WriteLine();
TestGetExcelColumnLetters(700);
TestGetExcelColumnLetters(701);
TestGetExcelColumnLetters(702);
TestGetExcelColumnLetters(703);
TestGetExcelColumnLetters(704);
TestGetExcelColumnLetters(705);
Console.ReadLine();
}
Kommentare zum Snippet