PHPExcel中列名的列索引

问题描述 投票:1回答:2

我刚刚开始使用PHPExcel-1.7.7 ..

我在从列名获取列索引时遇到问题,例如

A -> 1, H-> 8, L-> 12

像那样..

提前感谢..

php phpexcel
2个回答
5
投票

那是什么问题?

$column = 'IV';
$columnIndex = PHPExcel_Cell::columnIndexFromString($column);

为什么在最新的生产版本为1.7.9的情况下使用1.7.7版本?


0
投票
public static function lettersToInt(string $string): int {
    $letters = str_split($string);
    $lastLetter = array_pop($letters); // Last letter is defining number between 0-26
    $index = 0;
    $alphabet = range('A', 'Z');
    foreach($letters as $letter) {
        // Letters before last letter are defining how many times to add 26.
        $multiplier = array_search($letter, $alphabet) + 1;
        $index = $index + 26 * $multiplier;
    }
    $index = $index + array_search($lastLetter, $alphabet);
    return $index;
}

我已经测试过两个字母列名称。我的大脑拒绝解决三个字母(可能并不复杂,但是项目预算中没有更多时间了:))

© www.soinside.com 2019 - 2024. All rights reserved.