PHPExcel how to use data from reader

PHPphpexcel

I'm trying to understand how to get data from cells in PHPExcel but I have no idea. I read all documentation and I made this:

<?php
include('PHPExcel/Classes/PHPExcel/Reader/Excel2007.php');
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    public function readCell($column, $row, $worksheetName = '') {
        // Read title row and rows 20 - 30
        if ($row == 1 || ($row >= 20 && $row <= 30)) {
            return true;
        }

        return false;
    }
}
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadFilter( new MyReadFilter() );
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("sample_mymails222.xlsx");
print_r($objPHPExcel);
?>

print_r show very big array.
I think there is some functions to get data from cell in $objPHPExcel.

How to do it?

Thanx!

Best Answer

For anyone else who wants a more helpful answer, following the code in the example above, here is a quick snippet that will parse the active worksheet into a multi-dimensional array (this example ignores the ReadFilter that the OP created).

$data =  array();
$worksheet = $objPHPExcel->getActiveSheet();
foreach ($worksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);
  foreach ($cellIterator as $cell) {
    $data[$cell->getRow()][$cell->getColumn()] = $cell->getValue();
  }
}
var_dump($data);

BTW The reason I say "more helpful answer" is that I also had a hard time figuring out the PHPExcel documentation (which consists of 6 separate documents and no real API-like reference) the first time around.

The OP says he "read all the docs", but I am guessing that, like me, he read through the file called PHPExcel User Documentation - Reading Spreadsheet Files.doc which explains how to read in a spreadsheet, but does not cover using the contents.

To find that out, you have to dig into PHPExcel developer documentation.doc and get down to part 4 (which is where I found the example above).

Yup, my fault for not being thorough, but clearly this is a recurring issue. :-)