1、2、3、4 转换成 A、B、C、D
2016-06-02 11:32 张小驴同学 阅读(495) 评论(0) 编辑 收藏 举报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()); }
输出: