outputting cell data to an array

Topics: Developer Forum
Jul 5, 2008 at 8:05 AM
I have an excel 2007 workbook with two sheets that have only two columns which stand for first name and last name. The first sheet is used for girls and the second sheet is used for boys.
I want to export the cell data of  both cells to a two-dimensional array.
Here is my code:
include("PHPExcel.php");
include("PHPExcel/Reader/Excel2007.php");

$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$obj = $objReader->load("list.xlsx");

Now, here is my question, what properties and/or methods are used to retrieve the cells from A2 to A563 and from B2 to  B563 .
Thanks in advance
Developer
Jul 5, 2008 at 12:08 PM
Perhaps this will get you started:

$objReader = new PHPExcel_Reader_Excel2007();
$obj = $objReader->load('boys.xlsx');

$ws = $obj->getSheet(0);

$boys = array();
for ($row = 2; $row <= $ws->getHighestRow(); $row++) {
    $boys[] = array(
        'firstname' => $ws->getCellByColumnAndRow(0, $row)->getValue(),
        'lastname' => $ws->getCellByColumnAndRow(1, $row)->getValue(),
    );
}

var_dump($boys);
Jul 5, 2008 at 1:45 PM
Thank you. It helped very much.However, it generated an error when i ran the above code. But that was fixed after adding the following line before $obj=$objReader->load('boys.xlsx');
$objReader->setReadDataOnly(true);


Developer
Jul 5, 2008 at 3:27 PM
In any case, it may be a good idea to let the reader only read data like you did. I guess that it may speed up the script, but I'm not sure how big a difference it makes. Still, I'm curious why you are getting an error? Maybe you can attach a sample Excel file that generates the error. Which version of PHPExcel are you using?
Jul 6, 2008 at 8:02 AM
I have downloaded the last version (1.6.2) yesterday.This is the error:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 16 bytes) in C:\xampp\htdocs\PHPExcel\Cell.php on line 504
When I add ini_set("memory_limit","128M") at the top of my script, the error changes to:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1572864 bytes) in C:\xampp\htdocs\PHPExcel\Reader\Excel2007.php on line 502
But after adding setReaderDataOnly(true) the problem solves completely.
Coordinator
Jul 7, 2008 at 6:00 AM
Do you need formatting in the document? You can use setReadDataOnly(true) on the reader to only read cell values.