首页 > 解决方案 > Laravel:如何在使用模板文件导出时更改 startcell

问题描述

Laravel 框架 5.8.38
maatwebsite/excel 3.1.19

我正在尝试使用模板文件导出。
以下代码正在运行,但“startCell()”似乎不起作用。
它总是从导出文件中的“A6”开始。

在这种情况下我该怎么办。

class TestExport implements FromQuery, WithEvents, WithCustomStartCell, WithStrictNullComparison
{
    public function setTemplate(string $template_file)
    {
        if (file_exists($template_file)) {
            $this->template_file = $template_file;
        }
        return $this;
    }

    public function startCell(): string
    {
        return 'A3';
    }

    public function registerEvents(): array
    {
        return [
            BeforeExport::class => function (BeforeExport $event) {
                if (is_null($this->template_file)) {
                    return;
                }
                $event->writer->reopen(new LocalTemporaryFile($this->template_file), Excel::XLSX);
                $event->writer->getSheetByIndex(0);

                $this->calledByEvent = true;
                $event->writer->getSheetByIndex(0)->export($event->getConcernable());
                return $event->getWriter()->getSheetByIndex(0);
            },
            BeforeWriting::class => function (BeforeWriting $event) {
                $event->writer->removeSheetByIndex(1);
                return;
            },
        ];
    }
}

标签: phpexcellaravel

解决方案


推荐阅读