php PHPExcel如何从单元格获取列索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3458978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:50:50  来源:igfitidea点击:

PHPExcel how to get column index from cell

phpphpexcel

提问by john Griffiths

PHPExcel $cell->getColumn() returns 'A', 'B', 'C', ...

PHPExcel $cell->getColumn() 返回 'A', 'B', 'C', ...

which is the best way to get the integer (0, 1, 2, ...) from the cell.

这是从单元格中获取整数 (0, 1, 2, ...) 的最佳方法。

This function doesn't exist.

没有这个功能。

$colIndex = $cell->getColumnIndex();

So what is the alternative withoput converting chr to ascii ?

那么没有将 chr 转换为 ascii 的替代方法是什么?

回答by Mark Baker

$colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());

回答by Shad

You can get column index while iterate.

您可以在迭代时获取列索引。

$xls = PHPExcel_IOFactory::load($fn);
$xls->setActiveSheetIndex(0);
$sheet = $xls->getActiveSheet();

foreach($sheet->getRowIterator() as $row)
{
    foreach($row->getCellIterator() as $key => $cell)
    {
        echo $key; // 0, 1, 2...
        echo $cell->getCalculatedValue(); // Value here
    }
}

回答by Sarath Wijeshinghe

If you want to get decrement cell address using this function, you have to use another function with this function as follows.

<?php
echo columnLetter("AB");

function columnLetter($c){


$letter="";
    $c = intval(columnNumber($c));
    if ($c<=0) return '';

    while($c != 0){
       $p = ($c - 1) % 26;
       $c = intval(($c - $p) / 26);
       $letter = chr(65 + $p) . $letter;
    }

    return $letter;

}

function columnNumber($col){

    $col = str_pad($col,2,'0',STR_PAD_LEFT);
    $i = ($col{0} == '0') ? 0 : (ord($col{0}) - 64) * 26;
    $i += ord($col{1}) - 64;

    return $i-1;

}
?>