Created
June 16, 2023 14:18
-
-
Save LukeTowers/126b3c20bd0fe2c7b432c9453bbe3aca to your computer and use it in GitHub Desktop.
Winter CMS Excel fromView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace LukeTowers\Excel\Exports\Sheets; | |
use Illuminate\Contracts\View\View as ViewContract; | |
use Maatwebsite\Excel\Concerns\FromView; | |
use Maatwebsite\Excel\Concerns\WithEvents; | |
use Maatwebsite\Excel\Concerns\WithTitle; | |
use Maatwebsite\Excel\Events\AfterSheet; | |
use Maatwebsite\Excel\Concerns\ShouldAutoSize; | |
class ViewExampleSheet implements FromView, ShouldAutosize, WithTitle, WithEvents | |
{ | |
use \System\Traits\ViewMaker; | |
/** | |
* Data to pass to the view to be rendered | |
*/ | |
protected array $data; | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
$this->viewPath = $this->guessViewPath('/partials'); | |
} | |
public function view(): ViewContract | |
{ | |
// @TODO: Make it easier to get a ViewContract from the ViewMaker trait instead of this | |
return (new class($this) implements ViewContract { | |
protected $outer; | |
protected $vars = []; | |
public function __construct($outer) | |
{ | |
$this->outer = $outer; | |
} | |
public function render() | |
{ | |
return $this->outer->makePartial('sheet', $this->getData()); | |
} | |
public function name() | |
{ | |
return 'sheet'; | |
} | |
public function with($key, $value = null) | |
{ | |
if (is_array($key)) { | |
$this->vars = array_merge($this->vars, $key); | |
} elseif (isset($value)) { | |
$this->vars[$key] = $value; | |
} | |
return $this; | |
} | |
public function getData() | |
{ | |
return $this->vars; | |
} | |
})->with([ | |
'data' => $this->data, | |
]); | |
} | |
// | |
public function registerEvents(): array | |
{ | |
return [ | |
AfterSheet::class => function (AfterSheet $event) { | |
$event->sheet->getStyle('A2:A5')->applyFromArray([ | |
'font' => [ | |
'bold' => true, | |
], | |
]); | |
$event->sheet->getStyle('A6')->applyFromArray([ | |
'font' => [ | |
'italic' => true, | |
], | |
]); | |
}, | |
]; | |
} | |
/** | |
* Get the title of this sheet | |
*/ | |
public function title(): string | |
{ | |
return 'Index'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment