Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the algorithm to convert an Excel Column Letter into its Number?

Tags:

c#

math

excel

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

like image 842
David Basarab Avatar asked Mar 20 '09 20:03

David Basarab


People also ask

How do I convert a column name to a number in Excel?

In your Excel, click File > Options. In the Excel Options dialog box, select Formulas in the left pane. Under Working with formulas, check the R1C1 reference style box, and click OK.

How do I convert letters to numbers in Excel?

Select all the cells that you want to convert from text to numbers. Click on the yellow diamond shape icon that appears at the top right. From the menu that appears, select 'Convert to Number' option.

How do I get column letters in Excel formula?

Basic Formula - Works for A to Z You just have to input the number for the column directly in the formula. The CHAR() function gets a letter based on its number as defined by the character set of your computer; though, you don't need to know that to use this function.


1 Answers

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; } 
like image 66
Ian Nelson Avatar answered Sep 21 '22 11:09

Ian Nelson