Php – Ignore empty cells PHPExcel

PHPphpexcel

I'm using the library PHPExcel to read data in an Excel file. The problem I'm having, is that when I use something like:

$obj = PHPExcel_IOFactory::load($file);
$data = $obj->getActiveSheet()->toArray(null,true,true,true);

To load my file and convert its content into an array, I get all the columns and rows of my Excel file in my array even those without any data in them. Is there a method or something in the library PHPExcel to tell it to ignore cells in my Excel sheet that do not contain any data? (Instead of having a bunch of empty associative arrays in my $data)

Best Answer

If your problem is in getting empty columns that go after real data, and you would like to avoid these, you could do something like this:

$maxCell = $sheet->getHighestRowAndColumn();
$data = $sheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);

This will return array representing only the area containing real data.

Related Topic