I'm struggeling with Laravel-Excel 3.1. I'm not able to modify a file. My file has two sheets (called template). I want to load the document, modify it and let the user download it.
The Excel file was not created by me, I just need to fill it with data.
For exemple from this one :
to this one :

Thank you.
Well, not sure if this will still be helpful, but here some code to do this.
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
class TestExport implements WithEvents
{
use Exportable, RegistersEventListeners;
public static function beforeExport(BeforeExport $event)
{
// get your template file
$event->writer->reopen(new \Maatwebsite\Excel\Files\LocalTemporaryFile(storage_path('app/exports/report-front-page.xlsx')),Excel::XLSX);
$event->writer->getSheetByIndex(0);
// fill with information
$event->getWriter()->getSheetByIndex(0)->setCellValue('A1','Some Information');
$event->getWriter()->getSheetByIndex(0)->setCellValue('G4', 'Some Information');
return $event->getWriter()->getSheetByIndex(0);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With