Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active February 21, 2023 08:20
Show Gist options
  • Save unilecs/9816810892bda78415466dcb8b154a97 to your computer and use it in GitHub Desktop.
Save unilecs/9816810892bda78415466dcb8b154a97 to your computer and use it in GitHub Desktop.
using System;
public class Program
{
public static string ConvertToExcelTitle(int colNum)
{
string result = "";
while (colNum > 0)
{
// нумерация символов начинается с 0, в Excel c 1
colNum--;
// получаем код текущего символа
int lastSymbolCode = colNum % 26;
// преобразуем код в символ и добавляем в результат
result = (char)(lastSymbolCode + 65) + result;
// "двигаемся" к следующему символу
colNum /= 26;
}
return result;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(ConvertToExcelTitle(1)); // A
Console.WriteLine(ConvertToExcelTitle(28)); // AB
Console.WriteLine(ConvertToExcelTitle(701)); // ZY
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment