Laravel-excel: [QUESTION] Attach generated excel with V 3.1

Created on 22 Nov 2018  ·  8Comments  ·  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.1.7
  • Laravel version: 5.7.13
  • Package version: 3.1

Description

Hello,
how is it possible attach an excel file generated with the version 3.1?

In the previous version (v.2) it was possible through create method without "->download()"

I'm trying in this way, but it doesn't work:

$filename = 'text.xlsx';
$attachment = Excel::download(new TestExport(), $filename);

\Mail::to('[email protected]')->send(new SendMail($attachment));

# Inside SendMail.php
public function __construct($attachment)
{
    $this->attachment = $attachment;
}

public function build()
{

    $mail = $this->from('[email protected]')
        ->subject('test attachment')
        ->markdown('emails.test_mail')
        ->attach($this->attachment);

    return $mail;
}

Most helpful comment

I've found a solution to make it work without having to modify the Excel class and its protected method. You can call getFile() from the Excel::download(/**/). The code looks like so:

public function build()
    {
        return $this->markdown('emails.report')
            ->attach(
                Excel::download(
                    new AuditReport($this->audit), 
                    'report.xlsx'
                )->getFile(), ['as' => 'report.xlsx']
            );
    }

Excel::download() returns a BinaryFileResponse that's why it doesn't work directly, but you can grab the file.

Hope it helps 😃

All 8 comments

I have the exact same question.

I've found a solution changing from protected to public the export() method inside the file:

maatwebsite\excel\src\Excel.php

on row: 166

in this way, the code can be:
````
$attachment = Excel::export(new TestExport(), $filename);

[...]

$mail = $this->from('[email protected]')
->subject('test attachment')
->markdown('emails.test_mail')
->attach($this->attachment, , ['as' => $filename]);
````

I've found a solution to make it work without having to modify the Excel class and its protected method. You can call getFile() from the Excel::download(/**/). The code looks like so:

public function build()
    {
        return $this->markdown('emails.report')
            ->attach(
                Excel::download(
                    new AuditReport($this->audit), 
                    'report.xlsx'
                )->getFile(), ['as' => 'report.xlsx']
            );
    }

Excel::download() returns a BinaryFileResponse that's why it doesn't work directly, but you can grab the file.

Hope it helps 😃

I've found a solution to make it work without having to modify the Excel class and its protected method. You can call getFile() from the Excel::download(/**/). The code looks like so:

public function build()
    {
        return $this->markdown('emails.report')
            ->attach(
                Excel::download(
                    new AuditReport($this->audit), 
                    'report.xlsx'
                )->getFile(), ['as' => 'report.xlsx']
            );
    }

Excel::download() returns a BinaryFileResponse that's why it doesn't work directly, but you can grab the file.

Hope it helps

Watch out for the server's tmp dir though as the file gets written to disk.

Export should be a public method.

Thanks guys for helping each other out! 👍

Here is how I do it without downloading/creating the file:

use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;

...

$filename = "{$this->po->po_memo}.xlsx";
$attachment = Excel::raw(new PurchaseOrderLinesExport($this->po), BaseExcel::XLSX);
$subject = "Purchase Order Invoice"

return $this->from($this->po->employee->email)
            ->subject($subject)
            ->view('emails.pom.send')
            ->text('emails.pom.send')
            ->attachData($attachment, $filename);

I've found a solution to make it work without having to modify the Excel class and its protected method. You can call getFile() from the Excel::download(/**/). The code looks like so:

public function build()
    {
        return $this->markdown('emails.report')
            ->attach(
                Excel::download(
                    new AuditReport($this->audit), 
                    'report.xlsx'
                )->getFile(), ['as' => 'report.xlsx']
            );
    }

Excel::download() returns a BinaryFileResponse that's why it doesn't work directly, but you can grab the file.

Hope it helps smiley

Thanks for this, it worked for me.

use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;

...

$filename = "{$this->po->po_memo}.xlsx";
$attachment = Excel::raw(new PurchaseOrderLinesExport($this->po), BaseExcel::XLSX);

Thanks a lot. it's work for me

Was this page helpful?
0 / 5 - 0 ratings