public static int ToIndex(string columnName)
{
if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); } int index = ;
char[] chars = columnName.ToUpper().ToCharArray();
for (int i = ; i < chars.Length; i++)
{
index += ((int)chars[i] - (int)'A' + ) * (int)Math.Pow(, chars.Length - i - );
}
return index - ;
} public static string ToName(int index)
{
if (index < ) { throw new Exception("invalid parameter"); } List<string> chars = new List<string>();
do
{
if (chars.Count > ) index--;
chars.Insert(, ((char)(index % + (int)'A')).ToString());
index = (int)((index - index % ) / );
} while (index > ); return String.Join(string.Empty, chars.ToArray());
}
转自:https://www.cnblogs.com/lhlong/p/7920408.html