Laravel-excel: [问题]使用现有文件作为模板在导出之前附加新数据

创建于 2019-02-22  ·  5评论  ·  资料来源: Maatwebsite/Laravel-Excel

先决条件

版本号

  • PHP版本:7.2
  • Laravel版本:5.7
  • 套件版本:3.1

描述

我正在尝试编辑现有文件以用作模板。 我现在认为,对于此版本,此功能无法轻松使用。 但是我想知道是否可以使用插件中的函数并与function resisterEvents()一起玩,我自己就能做到。

我必须下载一个现有文件并选择一个工作表。 但不要对其进行编辑。

这是我的代码:

    /**
     * <strong i="16">@return</strong> array
     */
    public function registerEvents(): array
    {
        return [
            BeforeWriting::class  => function(BeforeWriting $event) {
                $event->writer->reopen(storage_path('app/public/files/mytemplate.xlsx'),Excel::XLSX);
                $event->writer->getSheetByIndex(0);
                return $event->getWriter()->getSheetByIndex(0);
            }
        ];
    }

有什么建议吗?

非常感谢

附加信息

我试图在function resisterEvents()内使用此代码在值中添加信息,但不起作用:

AfterSheet::class => function(AfterSheet $event) {
       $event->sheet->setCellValue('G2', 'Hello world');
}

并通过function query()附加数据

question

最有用的评论

->reopen()是一种内部方法,应谨慎使用。 您可能可以通过传递new LocalTemporaryFile(storage_path('app/public/files/mytemplate.xlsx'))重新打开来解决它。

在我们的路线图(版本3.3)上编辑现有文件(模板),但没有特定的发布日期。

如果您优先需要它,请查看我们的“商业支持”部分: https :

所有5条评论

最终,这对我来说适用于3.1.0版本。 在实际的版本3.1.10中,此功能不起作用,因为reopen函数已更改。 等待新的更新和编辑现有文件的改进。

->reopen()是一种内部方法,应谨慎使用。 您可能可以通过传递new LocalTemporaryFile(storage_path('app/public/files/mytemplate.xlsx'))重新打开来解决它。

在我们的路线图(版本3.3)上编辑现有文件(模板),但没有特定的发布日期。

如果您优先需要它,请查看我们的“商业支持”部分: https :

感谢您的快速答复。

我会尝试这种方式。

这不是当前的优先事项,但我正在测试,因为我需要使用宏和动态表将数据添加到现有的excel中,添加数据并重新计算excel公式。 我正在尝试所有这些,因为我知道使用复杂的excel很难做到这一点。

如果我发现好东西,我会报告给你

如果有更新,请尽快发表评论

你好,@ MarFelix。 我遇到了同样的问题,现在我正在使用一种解决方法。 这可能对您有帮助。
我深入研究了Mattwebsite代码,以了解导出是如何完成的。 因此,请记住用于下载文件的以下步骤:

  1. 调用Excel类以传递您的Export类(此示例使用download方法)
  2. Excel类标识应使用哪个Writer并在此Writer调用export方法。
  3. Writer打开您的Export (并调用BeforeExport事件),然后填充一个NEW工作表,然后调用其自己的write方法。
  4. write方法( Writer类)将调用BeforeWriting事件,并为XLSX以外的其他类型写入文件

因此,这就是BeforeWriting不起作用的原因,并且如果您在BeforeExport它,它将起作用,但是在新的工作表中。

要解决此问题,我在方法上添加了一个标志以获取导出数据(在我的情况下为FromCollection)。 这样,它就不会在步骤2上进行处理。然后,在BeforeWriting标记该标志,获取所需的工作表并在其上调用export方法。

那是代码:

public function collection()
{
    if ($this->calledByEvent) { // flag
        return $this->myCollectionToExport;
    }

    return collect([]);
}

public function registerEvents(): array
{
    return [
        BeforeWriting::class => function(BeforeWriting $event) {
            $templateFile = new LocalTemporaryFile(storage_path('app/public/files/mytemplate.xlsx'));
            $event->writer->reopen($templateFile, Excel::XLSX);
            $event->writer->getSheetByIndex(0);

            $this->calledByEvent = true; // set the flag
            $event->writer->getSheetByIndex(0)->export($event->getConcernable()); // call the export on the first sheet

            return $event->getWriter()->getSheetByIndex(0);
        },
    ];
}

采取一看downloadwrite的方法Maatwebsite\Excel\Writer ,这将是非常有益的。

有了这些信息和步骤,就有可能创建一个更复杂的解决方法。

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

相关问题

muhghazaliakbar picture muhghazaliakbar  ·  3评论

octoxan picture octoxan  ·  3评论

rossjcooper picture rossjcooper  ·  3评论

matthewslouismarie picture matthewslouismarie  ·  3评论

gamevnlc picture gamevnlc  ·  3评论