Skip to content

Commit

Permalink
Merge pull request #170 from rakibhoossain/patch-1
Browse files Browse the repository at this point in the history
CSV stream callback fixed
  • Loading branch information
freekmurze authored Apr 24, 2024
2 parents ce4b7e6 + 9520756 commit e2482b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,41 @@ foreach (range(1, 10_000) as $i) {
$writer->toBrowser();
```

You could also use a callback.

```php
use Spatie\SimpleExcel\SimpleExcelWriter;
use OpenSpout\Common\Entity\Row;

$writer = SimpleExcelWriter::streamDownload('user-list.xlsx', function ($writerCallback, $downloadName) {

$writerCallback->openToBrowser($downloadName);

$writerCallback->addRow(Row::fromValues([
'first_name' => 'First Name',
'last_name' => 'Last Name',
]));

$writerCallback->addRow(Row::fromValues([
'first_name' => 'Rakib',
'last_name' => 'Hossain',
]));

foreach (range(1, 10_000) as $i) {
$writerCallback->addRow(Row::fromValues([
'first_name' => 'Rakib',
'last_name' => 'Hossain',
]));

if ($i % 1000 === 0) {
flush();
}
}
});

$writer->toBrowser();
```


### Writing multiple rows at once

Expand Down
8 changes: 3 additions & 5 deletions src/SimpleExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ public static function streamDownload(

$writer = $simpleExcelWriter->getWriter();

if ($writerCallback) {
$writerCallback($writer);
}

$writer->openToBrowser($downloadName);
$writerCallback ?
$writerCallback($writer, $downloadName) :
$writer->openToBrowser($downloadName);

return $simpleExcelWriter;
}
Expand Down

0 comments on commit e2482b2

Please sign in to comment.