PHP code example of issei-m / streamed-csv-response

1. Go to this page and download the library: Download issei-m/streamed-csv-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

issei-m / streamed-csv-response example snippets


public function exportCustomersAction(Request $request)
{
    return new StreamedCsvResponse(
        // 1st parameter: any iterable CSV rows
        (function () {
            yield ['Full Name', 'Email', 'Gender'];

            foreach ($this->get('user_repository')->getAllUsers() as $user) {
                yield [
                    $user->getFullName(),
                    $user->getEmail(),
                    $user->getGender(),
                ];
            }

            // Of course, you can also use any iterable for cell representation
            yield (function () {
                yield '村澤 逸生';
                yield '[email protected]';
                yield '男性';
            })();
        })(),

        // 2nd parameter: the filename the browser uses in downloading 
        'customers.csv'
    ); 
}

$response = new StreamedCsvResponse($rows, 'customers.csv');
$response->setCharset('SJIS-win');

$response->send(); // Every cells are automatically encoded to SJIS-win.