Get Excel Column Key by Index

if(!function_exists('to_alphabet')){
	function to_alphabet($index){
		$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$length = strlen($str);

		$base = floor($index / $length);
		$remain = $index % $length;

		$c1 = '';
		$c2 = '';
		if($base > 0){
			$index = $base - 1;
			$c1 = substr($str, $index, 1);
		}

			$index = $remain;
			$c2 = substr($str, $index, 1);

		$c = $c1 . $c2;
		//logg($c);

		return $c;
	}
}

e.g.

  • 0 -> A
  • 25 -> Z
  • 26 -> AA
  • 29 -> AD