Laravel-excel: Excel load read the result only of the last column

Created on 7 Oct 2015  路  14Comments  路  Source: Maatwebsite/Laravel-Excel

I have a Excel file with 6 columns. When i use

$excel=Excel::load($destinationPath.'/'.$filename,function ($reader) {
    $reader->each(function($sheet) {
        // Loop through all rows
        $sheet->each(function($row) {
            // Here i supposed to get the values of 6 columns
            // But i got only the value of the last column
            dump($row); 
        });
    });
})

Can someone tell me what i did wrong or this is a bug ?

Most helpful comment

in the end , i have to use PHPExcel functions to get all information. For those who have the same problem like me

            $excel = [];

            Excel::load($destinationPath . $filename, function($reader) use (&$excel) {
                $objExcel = $reader->getExcel();
                $sheet = $objExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

                //  Loop through each row of the worksheet in turn
                for ($row = 1; $row <= $highestRow; $row++)
                {
                    //  Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                        NULL, TRUE, FALSE);

                    $excel[] = $rowData[0];
                }
            });

All 14 comments

If you have only 1 sheets, the first loop will be the rows. You can disable this behaviour in the config.

even if i do only import like this

$excel = Excel::load($destinationPath . $filename)->all()->toArray();
dd($excel);

I still got the result only of the last column. Im working on laravel 4.2 now.
The project on laravel 5.1 has the same plugin and import the same file properly.
So i dont even know what the problem is.

in the end , i have to use PHPExcel functions to get all information. For those who have the same problem like me

            $excel = [];

            Excel::load($destinationPath . $filename, function($reader) use (&$excel) {
                $objExcel = $reader->getExcel();
                $sheet = $objExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

                //  Loop through each row of the worksheet in turn
                for ($row = 1; $row <= $highestRow; $row++)
                {
                    //  Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                        NULL, TRUE, FALSE);

                    $excel[] = $rowData[0];
                }
            });

Thanks rpg999

Still re-occuring in Laravel 5.2.

Excel::load('file.xls', function($reader) {
// Getting all results
$results = $reader->get();
});

After pulling this the '$results' variable only returns a collection of the last column.

wish I came here sooner.. been trying to understand wth is going on for over an hour. I am too getting the same result.. trying to iterate through rows and only getting the last column.

I will try rpg999's code now

Edit: Thanks rpg999!! Was able to get a working solution going using your code! I hope this gets fixed.. and I hope your code doesn't break when it does get fixed :)

Will respond the same thing I responded last October:
"If you have only 1 sheets, the first loop will be the rows. You can disable this behaviour in the config."

Link to config: https://github.com/Maatwebsite/Laravel-Excel/blob/2.1/src/config/excel.php#L458

From the docs:

The ->get() and ->all() methods will return a sheet or row collection, depending on the amount of sheets the file has. You can disable this feature inside the import.php config by setting 'force_sheets_collection' to true. When set to true it will always return a sheet collection.

No need to change the package or mess with PHPExcel native methods. Just change the config setting ;)

I've tried setting the config to force sheets collection, and got absolutely no help,
but rpg999's idea works.

I see no change by making force_sheets_collection true over false, other than the result is wrapped by another object.

@rpg999's code works well though.

Because the reader use the first row as (heading) the attributes name of the result array. So you have to make the first row's column names valid, eg valid variable name.
Or you can set noHeading() for the reader, it will read all rows including the first row.
ref : http://www.maatwebsite.nl/laravel-excel/docs/import#extra

i try to import data from excel sheet but reader->all() function retrieve empty value

$data = Excel::load($path, function ($reader) {

        $results  = $reader->all();
        $firstrow = $reader->first();                                                                       
        dd($results);

        });

get the same problem, Everything is empty and still don't know why. My excel file has 3 sheets and 68 columns each sheet, maatwebsite load exactly 3 sheets and 68 columns, but all content is empty. I will try PHPExcel instead.

Excel::load($file)->noHeading()->all()->toArray();

noHeading() works

@kingconan
Your solution saved my day. Thank you!

Was this page helpful?
0 / 5 - 0 ratings