static void Main(string[] args) { Console.WriteLine("1 :" + ColNumnerToColCode(1)); Console.WriteLine("2 :" + ColNumnerToColCode(2)); Console.WriteLine("3 :" + ColNumnerToColCode(3)); Console.WriteLine("4 :" + ColNumnerToColCode(4)); Console.WriteLine("27 :" + ColNumnerToColCode(27)); Console.WriteLine("36 :" + ColNumnerToColCode(36)); Console.WriteLine("703:" + ColNumnerToColCode(703)); Console.Read(); } /// <summary> /// 将列号转换为Excel中的列字母 /// </summary> /// <param name="index"></param> /// <returns></returns> static string ColNumnerToColCode(int index) { if (index <= 0) throw new Exception("invaild parameter"); index--; List<string> chars = new List<string>(); do { if (chars.Count > 0) index--; chars.Insert(0, ((char)(index % 26 + (int)‘A‘)).ToString()); index = (int)((index - index % 26) / 26); } while (index > 0); return String.Join(string.Empty, chars.ToArray()); }
输出:
时间: 2024-10-28 15:00:19