Question

I want to get cell value from a specific column in the row object:

$this->parsedExcelFile; //this is the worksheet
    $rows = $this->parsedExcelFile->getActiveSheet()->getRowIterator(($this->startRowIndex+1));

    foreach($rows as $row){
        $values[] = $row->getCellFromColumn('B')->getValue();
        //in getCellFromColumn is the column index, lets say B
        //getCellFromColumn($par) is a fictional function, i can't find how to get cell from a 
        //certain column directly from the row object

    }

I don't want to get it with:

$this->parsedExcelFile->getCell('B'.$row->getRowIndex())->getValue();

And i don't want to loop trough all cells. Any help is welcome.

Was it helpful?

Solution

Add this function to the PHPExcel_Worksheet_Row class

public function getColumnValue($column = 0) {
    if(is_int($column)) {
        return $this->_parent->getCellByColumnAndRow($column, $this->_rowIndex);
    }  

    return $this->_parent->getCell($column.$this->_rowIndex);
}

Subsequently, call the new function on each row object returned by the iterator, $row->getColumnValue($column)->getValue();, where $column is either the column index or the column alphabet.

EDIT: The PHPExcel_Worksheet_Row class is in the file row.php in directory Classes->PHPExcel->Worksheet.

OTHER TIPS

I doubt there is any function getCellFromColumn() in phpExcel library. You can use the way it is retrieved in PHPExcel Column Loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top