C#根据数字获取Excel列名

        /// <summary>
        /// 获取Excel列名
        /// </summary>
        /// <param name="columnNumber">从1开始的数字</param>
        /// <returns>列的序号,如:A、B、C、AA、BB</returns>
        public static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;
            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }
            return columnName;
        }

 

posted @ 2022-09-08 13:53  来瓶冰镇可乐吧  阅读(479)  评论(0编辑  收藏  举报