在 Laravel-Excel 3.0 中设置活动 Sheet
Set Active Sheet in Laravel-Excel 3.0
我使用以前版本的 Laravel-Excel to export data and in the past I've been able to use $sheets->setActiveSheetIndex(0)->download('xls');
(link to PHPSpreadsheet documentation) 来设置用户打开文件时的活动选项卡。
在 3.0 版本中,我不知道该把它放在哪里。如果我不尝试设置活动 sheet,文件就会下载,因此其余代码有效。我已经尝试将它添加到导出控制器中,如下所示,但它会抛出错误 Call to undefined method App\Exports\TechMatrixExport::setActiveSheetIndex()
。在下面的示例中,我希望 TechnologiesSheet 在用户打开文件时处于活动状态。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class TechMatrixExport implements WithMultipleSheets
{
use Exportable;
public function sheets(): array
{
$sheets = [];
$sheets[] = new TechnologiesSheet();
$sheets[] = new NotesSheet();
$sheets[] = new InputsSheet();
$sheets[] = new ReferencesSheet();
return $sheets;
}
}
控制器:
public function __construct(\Maatwebsite\Excel\Excel $excel)
{
$this->excel = $excel;
}
public function exportAll()
{
return (new TechMatrixExport)->setActiveSheetIndex(0)->download('tech_matrix.xlsx');
}
->setActiveSheetIndex(0);
属于哪里?
您应该使用 BeforeWriting (https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html#available-events) 来设置 ActiveSheetIndex。
我在你的代码中添加了 RegistersEventListeners,我在我的项目中以这种方式工作,如果这对你有用,请告诉我。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
class TechMatrixExport implements WithMultipleSheets, WithEvents
{
use Exportable, RegistersEventListeners;
public function registerEvents(): array
{
return [
// Handle by a closure.
BeforeExport::class => function(BeforeExport $event) {
$event->writer->getProperties()->setCreator('You')->setTitle("Title");
},
BeforeWriting::class => function(BeforeWriting $event) {
$event->writer->setActiveSheetIndex(0);
},
];
}
public function sheets(): array
{
$sheets = [];
$sheets[] = new TechnologiesSheet();
$sheets[] = new NotesSheet();
$sheets[] = new InputsSheet();
$sheets[] = new ReferencesSheet();
return $sheets;
}
}
我使用以前版本的 Laravel-Excel to export data and in the past I've been able to use $sheets->setActiveSheetIndex(0)->download('xls');
(link to PHPSpreadsheet documentation) 来设置用户打开文件时的活动选项卡。
在 3.0 版本中,我不知道该把它放在哪里。如果我不尝试设置活动 sheet,文件就会下载,因此其余代码有效。我已经尝试将它添加到导出控制器中,如下所示,但它会抛出错误 Call to undefined method App\Exports\TechMatrixExport::setActiveSheetIndex()
。在下面的示例中,我希望 TechnologiesSheet 在用户打开文件时处于活动状态。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class TechMatrixExport implements WithMultipleSheets
{
use Exportable;
public function sheets(): array
{
$sheets = [];
$sheets[] = new TechnologiesSheet();
$sheets[] = new NotesSheet();
$sheets[] = new InputsSheet();
$sheets[] = new ReferencesSheet();
return $sheets;
}
}
控制器:
public function __construct(\Maatwebsite\Excel\Excel $excel)
{
$this->excel = $excel;
}
public function exportAll()
{
return (new TechMatrixExport)->setActiveSheetIndex(0)->download('tech_matrix.xlsx');
}
->setActiveSheetIndex(0);
属于哪里?
您应该使用 BeforeWriting (https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html#available-events) 来设置 ActiveSheetIndex。
我在你的代码中添加了 RegistersEventListeners,我在我的项目中以这种方式工作,如果这对你有用,请告诉我。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
class TechMatrixExport implements WithMultipleSheets, WithEvents
{
use Exportable, RegistersEventListeners;
public function registerEvents(): array
{
return [
// Handle by a closure.
BeforeExport::class => function(BeforeExport $event) {
$event->writer->getProperties()->setCreator('You')->setTitle("Title");
},
BeforeWriting::class => function(BeforeWriting $event) {
$event->writer->setActiveSheetIndex(0);
},
];
}
public function sheets(): array
{
$sheets = [];
$sheets[] = new TechnologiesSheet();
$sheets[] = new NotesSheet();
$sheets[] = new InputsSheet();
$sheets[] = new ReferencesSheet();
return $sheets;
}
}