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
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With