C#: Convert spreadsheet column letters to numbers


Spreadsheet software like MS Excel and Calc use letters to label its columns. They start from “A” and continue to “Z”, “AA” and so on. You may recognise this as base-26. From right-to-left, each letter (or digit), has the value of 26 to the power of its zero-indexed position.

So, For “ABC”, you have:

C = 3 * (26 ^ 0) = 3 * 1 = 3
B = 2 * (26 ^ 1) = 2 * 26 = 52
A = 1 * (26 ^ 2) = 1 * 676 = 676

ABC = 3 + 52 + 676
ABC = 731

 

This is my implementation for converting column-letters and row-numbers to X-and-Y coordinates.

The code was a pretty straight-forward exercise. I did make a point of not using regex. I’ll include the class below (as always).

 

The usage is as follows:


SpreadsheetCoord.ConvertToCoord("ABC1");
SpreadsheetCoord.ConvertToColumnCode(731, 1);

 

Well, that’s is.

I hope someone finds this useful.
 

Code Repository

Social Media

 Share Tweet

Categories

Programming

Tags

.NET C# MS Excel

Post Information

Posted on Wed 10th Feb 2021

Modified on Sat 5th Aug 2023