0

I want to know if it's possible to get a cell by its name in an xls document, I mean Ii have this info in a excel file:

enter image description here

Normally to get the coordinate of the cell with the value "ASUS"

$objPHPExcel->getActiveSheet()->getCell('B3')->getValue(); 

my problem is that users send my this excel file, and sometimes the rows are in disorder, e.g the row B3 sometimes appear in a different row like "B6" or "B7" or "B5", how I can get the cell "ASUS" getting by cell name "Modelo"

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Juan Castillo
  • 103
  • 2
  • 9

2 Answers2

2

There is nothing built-in to PHPExcel to do a search, but using the following example, it can do well for your problem.

$foundInCells = array();
$searchTerm = 'ASUS';

foreach ($objPHPExcel->getWorksheetIterator() as $CurrentWorksheet) {
 $ws = $CurrentWorksheet->getTitle();

 foreach ($CurrentWorksheet->getRowIterator() as $row) {

    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);

    foreach ($cellIterator as $cell) {

        if ($cell->getValue() == $searchTerm) {

            $foundInCells[] = $ws . '!' . $cell->getCoordinate();
            }
        }
    }
}
var_dump($foundInCells);
0

You'd have to loop through the entire file to find it just like searching a value in a 2D array.

Take a look at this answer

Community
  • 1
  • 1
Diego Fu
  • 410
  • 2
  • 10