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

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

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

Message par ForumBot »

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
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

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

Message par ForumBot »

```
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 »