-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from Eloar/excel_v3
Maatwebsite/Excel v3 support through new component
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ Laravel 4.X, Laravel 5.X and Laravel 6 are supported. | |
|
||
* Laravel 4.X / 5.X / 6.X | ||
* laravelcollective/html package if you use Laravel5.X | ||
* php 5.4+ | ||
* php 5.5+ | ||
|
||
## Installation | ||
|
||
|
@@ -433,7 +433,7 @@ If you discover any security related issues, please email [email protected] instea | |
|
||
## License | ||
|
||
© 2014—2019 Vitalii Stepanenko | ||
© 2014—2022 Vitalii Stepanenko | ||
|
||
Licensed under the MIT License. | ||
|
||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Nayjest\Grids\Components; | ||
|
||
|
||
use Generator; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Maatwebsite\Excel\Concerns\FromGenerator; | ||
use Maatwebsite\Excel\Concerns\WithEvents; | ||
use Maatwebsite\Excel\Concerns\WithHeadings; | ||
use Maatwebsite\Excel\Events\BeforeSheet; | ||
use Maatwebsite\Excel\Excel; | ||
use Maatwebsite\Excel\Exporter; | ||
use Nayjest\Grids\Grid; | ||
|
||
|
||
/** | ||
* When using Maatwebsite/Excel v3 (or newer) which uses PhpSpreadsheet in place of PhpExcel | ||
* use this export component in place of ExcelExport component. It provides same funcionality | ||
* but uses newer API | ||
* | ||
* Be careful this file is compatible with php version 7 and above. Don't try to use this | ||
* component in projects using older PHP versions. | ||
* | ||
* @author: Janusz Paszynski | ||
* @package Nayjest\Grids\Components | ||
*/ | ||
class PhpSpreadsheetExport extends ExcelExport implements FromGenerator, WithHeadings, WithEvents | ||
{ | ||
/** @var Exporter */ | ||
protected $exporter; | ||
|
||
public function __construct() | ||
{ | ||
if(!interface_exists('Maatwebsite\Excel\Exporter')) { | ||
throw new \RuntimeException('It seems there is no Maatwebsite v3 installed. Install it or use ExcelExport component'); | ||
} | ||
} | ||
|
||
public function initialize(Grid $grid) | ||
{ | ||
parent::initialize($grid); | ||
$this->exporter = \app(Exporter::class); | ||
} | ||
|
||
/** @inheritDoc */ | ||
protected function renderExcel() | ||
{ | ||
$response = $this->exporter->download($this, sprintf('%s.%s', $this->getFileName(), $this->getExtension()), Excel::XLSX); | ||
throw new HttpResponseException($response); | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function generator(): Generator | ||
{ | ||
$columnsConfig = $this->grid->getConfig()->getColumns(); | ||
$provider = $this->grid->getConfig()->getDataProvider(); | ||
$this->resetPagination($provider); | ||
$provider->reset(); | ||
while($row = $provider->getRow()) { | ||
$output = []; | ||
foreach($columnsConfig as $column) { | ||
if ($this->isColumnExported($column)) { | ||
$output[] = $this->escapeString($column->getValue($row)); | ||
} | ||
} | ||
yield $output; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @return array | ||
*/ | ||
function headings(): array | ||
{ | ||
return $this->getHeaderRow(); | ||
} | ||
|
||
/** | ||
* | ||
* @return array | ||
*/ | ||
function registerEvents(): array | ||
{ | ||
return [ | ||
BeforeSheet::class => function(BeforeSheet $event) { | ||
$sheetTitle = $this->getSheetName(); | ||
if ($sheetTitle) { | ||
$event->sheet->setTitle($sheetTitle); | ||
} | ||
} | ||
]; | ||
} | ||
|
||
} |