Laravel-excel: [问题] 用 V 3.1 附加生成的 excel

创建于 2018-11-22  ·  8评论  ·  资料来源: Maatwebsite/Laravel-Excel

版本

  • PHP 版本:7.1.7
  • Laravel 版本:5.7.13
  • 包版本:3.1

描述

你好,
如何附加使用 3.1 版生成的 excel 文件?

在以前的版本 (v.2) 中,可以通过不带“->download()”的 create 方法

我正在以这种方式尝试,但它不起作用:

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

最有用的评论

我找到了一种无需修改 Excel 类及其受保护方法即可使其工作的解决方案。 您可以从Excel::download(/**/)调用getFile() Excel::download(/**/) 。 代码如下所示:

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

Excel::download()返回BinaryFileResponse这就是它不能直接工作的原因,但您可以获取文件。

希望能帮到你😃

所有8条评论

我有完全相同的问题。

我找到了一个解决方案,将文件中的 export() 方法从 protected 更改为 public:

maatwebsite\excel\src\Excel.php

在行:166

这样,代码可以是:
````
$attachment = Excel::export(new TestExport(), $filename);

[...]

$mail = $this->from('[email protected]')
-> 主题('测试附件')
->markdown('emails.test_mail')
->attach($this->attachment, , ['as' => $filename]);
````

我找到了一种无需修改 Excel 类及其受保护方法即可使其工作的解决方案。 您可以从Excel::download(/**/)调用getFile() Excel::download(/**/) 。 代码如下所示:

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

Excel::download()返回BinaryFileResponse这就是它不能直接工作的原因,但您可以获取文件。

希望能帮到你😃

我找到了一种无需修改 Excel 类及其受保护方法即可使其工作的解决方案。 您可以从Excel::download(/**/)调用getFile() Excel::download(/**/) 。 代码如下所示:

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

Excel::download()返回BinaryFileResponse这就是它不能直接工作的原因,但您可以获取文件。

希望能帮助到你

当文件被写入磁盘时,请注意服务器的 tmp 目录。

导出应该是一个公共方法。

谢谢大家互相帮助! 👍

这是我在不下载/创建文件的情况下执行此操作的方法:

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

我找到了一种无需修改 Excel 类及其受保护方法即可使其工作的解决方案。 您可以从Excel::download(/**/)调用getFile() Excel::download(/**/) 。 代码如下所示:

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

Excel::download()返回BinaryFileResponse这就是它不能直接工作的原因,但您可以获取文件。

希望对笑脸有帮助

谢谢你,它对我有用。

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

非常感谢。 这对我有用

此页面是否有帮助?
0 / 5 - 0 等级