Getting cells by coordinates

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

    foreach ($worksheet->getRowIterator() as $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);
            // I wish
            echo $cellIterator->getCell("A3"); // row: $row, cell: A3
    }
}

I am looking for a similar method, which I named getCellabove or well-written PHPExcel documentation.

Thank.

+5
source share
3 answers

If you have information $rowfrom RowIterator, you can simply call:

$rowIndex = $row->getRowIndex ();
$cell = $sheet->getCell('A' . $rowIndex);
echo $cell->getCalculatedValue();

Full code:

foreach($worksheet->getRowIterator() as $row){
    $rowIndex = $row->getRowIndex();

    $cell = $worksheet->getCell('A' . $rowIndex);
    echo $cell->getCalculatedValue();
    $cell = $worksheet->getCell('B' . $rowIndex);
    echo $cell->getCalculatedValue();
}
+13
source

This is what I need:

function coordinates($x,$y){
 return PHPExcel_Cell::stringFromColumnIndex($x).$y;
}

implementation:

coordinates(5,7); //returns "E7"

Although this could be done for the AZ columns:

function toNumber($dest)
{
    if ($dest)
        return ord(strtolower($dest)) - 96;
    else
        return 0;
}

function lCoordinates($x,$y){
 $x = $toNumber($x);
 return PHPExcel_Cell::stringFromColumnIndex($x).$y;
}

implementation:

lCoordinates('E',7); //returns "E7"
+6
source

, , rangeToArray() , array_intersect_key() , :

$worksheet = $objPHPExcel->getActiveSheet();
$highestColumn = $worksheet->getHighestColumn();

$columns = array_flip(array('A','C','E'));
foreach($worksheet->getRowIterator() as $row)
{
    $range = 'A'.$row->getRowIndex().':'.$highestColumn.$row->getRowIndex();
    $rowData = $worksheet->rangeToArray( $range, 
                                         NULL, 
                                         TRUE,
                                         TRUE,
                                         TRUE);
    $rowData = array_intersect_key($rowData[$row->getRowIndex()],$columns);
    //  do what you want with the row data
}

The latest SVN code introduces a number of new methods for iterators, including the ability to work with ranges or set a pointer to specific rows and columns

+1
source

All Articles