Skip to content

Commit

Permalink
Merge pull request #226 from Eloar/excel_v3
Browse files Browse the repository at this point in the history
Maatwebsite/Excel v3 support through new component
  • Loading branch information
Nayjest authored Oct 23, 2022
2 parents 3fee310 + dbece8d commit 0e6671e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"illuminate/support": ">=4.2",
"nayjest/builder": "~2"
},
"require-dev": {
"laravel/framework": "^5.5.0"
},
"suggest": {
"laravelcollective/html": "Required to work with Laravel 5.X",
"maatwebsite/excel": "Required to work with Excel export component"
Expand Down
5 changes: 5 additions & 0 deletions src/Components/ExcelExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ protected function renderExcel()
{
/** @var Excel $excel */
$excel = app('excel');

if (!method_exists($excel, 'create')) {
throw new \RuntimeException('U are most likely using Maatwebsite/Excel v3 or newer. Use PhpSpreadsheetExport component instead');
}

$excel
->create($this->getFileName(), $this->getOnFileCreate())
->export($this->getExtension());
Expand Down
96 changes: 96 additions & 0 deletions src/Components/PhpSpreadsheetExport.php
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);
}
}
];
}

}

0 comments on commit 0e6671e

Please sign in to comment.