Home » Php » Php split / chunk Excel with Maatwebsite Laravel-Excel v.3

Php split / chunk Excel with Maatwebsite Laravel-Excel v.3

Posted by: admin February 25, 2020 Leave a comment


I have very big xlsx 1000000 rows, but for testing I use Excel with 999 rows. So, to save memory I want to take chunks of data from this file and handle them. I found this https://docs.laravel-excel.com/3.1/imports/chunk-reading.html

namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithChunkReading;
class ProducttestImport implements ToArray,WithChunkReading
    public function array(array $row): array
    { return $row ; }
    public function chunkSize(): int
    { return 100; }

in controller

$rows = Excel::toArray(new ProducttestImport(), $path.$excelName)[0];
return  $rows;

but seems I’m missing something, because $rows is having array with 999 arrays. I need array with 10 arrays (when chunking 999 rows).

Where I can access/get those chunks?

How to&Answers:


namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
class ProducttestExport implements FromArray
{use Exportable;
    public $inputArr;

    public function __construct(array $inputArr = [])
    { $this->inputArr = $inputArr; }

    public function array() :array
        if(count($this->inputArr) >0) return $this->inputArr;
        return [];

In foreach

Excel::store( new ProducttestExport($array), $key.'_file.xlsx', 'my_disk' );

Then handle files one by one..

But anyway I hope someone knows how to get chunks and will post an answer.