c#
Get Excel Column Name in csharp
A
Desh-Duniya Team
Author
public static string GetExcelColumn(string col_name, int IncrementValue)
{
string excelCol = string.Empty;
int result = 0;
// Process each letter.
for (int i = 0; i < col_name.Length; i++)
{
result *= 26;
char letter = col_name[i];
// See if it's out of bounds.
if (letter < 'A') letter = 'A';
if (letter > 'Z') letter = 'Z';
// Add in the value of this letter.
result += (int)letter - (int)'A' + 1;
}
excelCol = GetExcelColumnName(result + IncrementValue);
return excelCol;
}
private static string GetExcelColumnName(int col_num)
{
// See if it's out of bounds.
if (col_num < 1) return "A";
// Calculate the letters.
string result = "";
while (col_num > 0)
{
// Get the least significant digit.
col_num -= 1;
int digit = col_num % 26;
// Convert the digit into a letter.
result = (char)((int)'A' + digit) + result;
col_num = (int)(col_num / 26);
}
return result;
}