C#语言中通过列名获取该列在excel中的索引

通过列名获取该列在excel中的索引

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}

示例:

1
2
3
int bIndex = "B".ExcelColumnIndex();

int amIndex = "AM".ExcelColumnIndex();
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×