Qu'est-ce que the algorithm to convert an Excel Column Letter into its Number?

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Qu'est-ce que the algorithm to convert an Excel Column Letter into its Number?

Message par Clippy »

I need an algorithm to convert an Excel Column letter to its proper number.


The language this will be written in is C#, but any would do or even pseudo code.


Please note I am going to put this in C# and I don’t want to use the office dll.


For ‘A’ the expected result will be 1


For ‘AH’ = 34


For ‘XFD’ = 16384

ayi
Site Admin
Messages : 9242
Inscription : mer. 22 avr. 2026, 17:21

Re: Qu'est-ce que the algorithm to convert an Excel Column Letter into its Number?

Message par ayi »

public static int ExcelColumnNameToNumber(string columnName)
{
if (string.IsNullOrEmpty(columnName)) throw new ArgumentNullException("columnName");

columnName = columnName.ToUpperInvariant();

int sum = 0;

for (int i = 0; i < columnName.Length; i++)
{
sum *= 26;
sum += (columnName[i] - 'A' + 1);
}

return sum;
}

Répondre

Revenir à « Excel & VBA »