计算Excel列号的算法!

2010-04-01 17:13  1062人阅读  评论 (0)

今天碰到这个顿时晕了一把,后来明白过来了

function number2col($number) {
    $number--;
    $str = "";
    while($number >= 0) {
        $str = chr($number % 26 + 65) . $str;
        $number = intval($number / 26) - 1;
    };
    return $str;
}

function col2number($str) {
    $str = strtoupper($str);
    for ($number = 0, $len = strlen($str), $i = 0; $i < $len; $i++) {
        $number += pow(26, $i) * (ord($str{$len - $i - 1}) - 64);
    }
    return $number;
}