Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Excel 3.1 : Import, modify, download

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 : enter image description here to this one : enter image description here

Thank you.

like image 968
Ayoub TMT Avatar asked Dec 28 '25 15:12

Ayoub TMT


1 Answers

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);
    }   

}
like image 121
Luccas Rafael Avatar answered Dec 30 '25 07:12

Luccas Rafael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!