ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

ExcelColumnIndex

Returns the excel column index from a column name

Source

public static int ExcelColumnIndex(this string columnName)
{
	int number = 0;
	int pow = 1;

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

	return number;
}

Example

int bIndex = "B".ExcelColumnIndex();

int amIndex = "AM".ExcelColumnIndex();

Author: Victor Rodrigues

Submitted on: 13 mei 2009

Language: C#

Type: System.String

Views: 4535