TL; DR: This article introduces Laravel’s FastExcel component, gives a quick look at PHP generators, and shows how to combine the two to generate Excel files from data sets while saving memory.

About FastExcel

Laravel FastExcel is designed to be a Laravel style Spout with the goal of simplifying import/export. It can be seen as a faster (and more memory-friendly) alternative to Laravel Excel, but in a different way and with fewer features. It is implemented in two steps.

First, install by composer:

composer require rap2hpoutre/fast-excelCopy the code

Then, export a Model or Collection to XLSX, CSV or ODS:

fastexcel($collection) - >export('file.xlsx');Copy the code

See the README of the Project Homepage for more details.

Generators (Generators)

Generators were introduced many years ago in PHP 5. A “generator” function is much like a normal function, except that instead of returning a definitive value, it is “generator” yields values that you can iterate over based on your requirements.

One goal of such a function is to delay iterating through the data without building an array. As a result, memory can be saved when dealing with large data sets. Please refer to the PHP documentation for details:

Generators allow you to write code in a foreach block to iterate over a set of data without having to build an array in memory. Because that might cause memory limits to be exceeded or require a lot of processing time to generate.

Suppose you now have a User model with 10M+ entries in the database and you want to iterate over them in code. Instead of calling User::all(), you can also use “generator” :

function usersGenerator() {
    foreach (User::cursor() as $user) {
        yield $user; }}$users = usersGenerator();
foreach($users as $user) {
    // Do something with each user without hitting memory limit
}

Copy the code

The above example fetches user data one by one while running the query. It simply uses the memory required to load a user N times.

Export large data sets using FastExcel and Generators

As of V1.3.0, FastExcel accepts a generator function as a parameter. Following the previous example, you can pass the generator to the FastExcel function:

function usersGenerator() {
    foreach (User::cursor() as $user) {
        yield $user;
    }
}

// Export consumes only a few MB, even with 10M+ rows.
$users = usersGenerator();
fastexcel($users) - >export('test.xlsx');Copy the code


FastExcel creates generators line by line for internal use, so no extra memory is consumed. This can be a time-consuming operation, so make sure you don’t hit the max_execution_time limit (you can use queues, any asynchronous technique, increase the maximum execution time, or even execute from the CLI). Still, be careful not to use up all of your server’s memory with one export operation.

So, with the help of “Generator,” you can now export thousands of model data to XLSX, CSV, and ODS files in a few lines of code in a Laravel project.

For more information on this component, go to github.com/rap2hpoutre…


Laravel tutorial: Use Fast Excel to solve the memory problem caused by exporting large XLSX files (tens of millions of levels)


To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.