通过行的索引,转成Excel的字符属性格式 (如:A,AC,AE等) - ExcelColNameGet

2017-06-02 15:04:50  访问(1397) 赞(0) 踩(0)

  • 
    
            /// <summary>
            /// 通过行的索引,转成Excel的字符属性格式 (如:A,AC,AE等) - ExcelColNameGet
            /// </summary>
            /// <param name="colIndex">行索引,从0开始</param>
            /// <returns></returns>
            protected string ExcelColNameGet(int colIndex)
            {
                // 变成从1开始 //
                int columnIndex = colIndex + 1;
    
                // 如果索引项小于等于0,不符合逻辑 //
                if (columnIndex <= 0)
                {
                    return "";
                }
    
                string theResult = "";
                int charA = (int)'A';
                int modV = 0;
                char pC = 'A';
    
                do
                {
                    --columnIndex;
    
                    modV = columnIndex % 26;
                    pC = (char)(modV + charA);
    
                    theResult = pC.ToString() + theResult;
    
                    columnIndex = (int)((columnIndex - modV) / 26);
    
                } while (columnIndex > 0);
    
                return theResult;
            }
    
    

  • 输入1:
    输出:

上一条:

下一条:


 
 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)