Laravel Excel: How to append rows to an existing Excel file

Search for a command to run...

No comments yet. Be the first to comment.
In this series/category, I would share with you guys all of my exp and best practices, to build the awesome products
Hey guys, We all know that Cache facade plays a good role across the Laravel application lifecycle, isn't it? Cache simply boosts up the requests by retaining the computed data from file storage or memory (aka RAM) depending on our configuration. Tod...
Hey guys, An update after I posted the “Laravel Excel: How to append rows to an existing Excel file“ a while back. This post will give you another solution and probably a better way to export that fits every case. Problems Previously, when trying to ...

Hey guys, An easy topic for today! Make something a default (e.g., category, product, campaign, etc). Let’s go with the categories table for the examples. Let’s say we only want one category to be the default one. And find out which is the optimal wa...
Hey guys, Probably a chapter 2 of this https://sethphat.dev/why-is-wordpress-still-an-awesome-cms blog post, haha. Here was the story: I’ve been spending like 2 days figuring out which CMS I want to use for my new blog (for my new hobby lol) Let’s se...

Hey guys, Recently, I just bought Herd PRO (a yearly subscription). I’ve been using the basic Herd since the first release. So, my latest work required interaction with S3 storage, there are several ways to achieve that in the local env: Use Cloud (...

Hey guys,
Well damn, it has been a while since my last post lol, lazy & busy. But here I am, back to write useful tips for y’all 🥹
Let us do some exporting tasks for today, using Laravel Excel to export XLSX (yeah CSV is so 2010 lol). But, append more rows to an existing file.
From your export class (e.g.: UserExport), implements the WithEvents
Once you added the registerEvents method, add this
public function registerEvents(): array
{
return [
BeforeWriting::class => function (BeforeWriting $event) {
if (!Storage::exists($this->filePath)) {
return;
}
file_put_contents(
$tempFile = tempnam('/tmp', 'export') . '.xlsx',
Storage::get($this->filePath)
);
$templateFile = new LocalTemporaryFile($tempFile);
$event->writer->reopen($templateFile, Excel::XLSX);
$event->writer->getSheetByIndex(0)
->export($event->getConcernable());
return $event->writer->getSheetByIndex(0);
},
];
}
It will retrieve the file from your desired storage (local, public, s3, etc), store to tmp folder (which is available for both server & serverless environment), and tell Laravel Excel to use that file to write more rows.
Simple right?
And yeah, I know, it’s a shame that reading an existing file is not available in the package.
Yep, each job can write around 500 ~ 1000 rows, then store the file, and dispatch another job to continue until it writes all rows.
Reduce memory-leak or timeout issues, since jobs only handle a small amount of data.
Thanks to Storage from Laravel, this can be done super easy.
With the code above, I’ve read the file, and to write, simply:
Excel::store(
new UserExport(collect($users), $this->filePath),
$this->filePath
);
Yep, tested on both server & serverless (Lambda), lovely!
Thanks for reading and have fun!