用递归函数将数字转换为EXCEL中的列名,直接上代码(PHP):
1 //递归求解 2 function R_NumToLetter($num) 3 { 4 if($num == 0) 5 { 6 return ''; 7 } 8 $base = 26; 9 $mod = (int)($num % $base); 10 $num = (int)($num / $base); 11 if($mod == 0) 12 return R_NumToLetter($num - 1).NumToLetter($base); 13 if($num == 0) 14 return NumToLetter($mod); 15 return R_NumToLetter($num).NumToLetter($mod); 16 } 17 18 function NumToLetter($num) 19 { 20 if($num == 0) 21 return ''; 22 $num = (int)$num - 1; 23 $ord_A = ord('A'); 24 $chr = chr($ord_A + $num); 25 return $chr; 26 }