Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use phpoffice/phpspreadsheet for Excel Export #1105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Grid/Export/PHPExcel2003Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

namespace APY\DataGridBundle\Grid\Export;

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
* PHPExcel_Excel 2003 Export (.xlsx).
*/
class PHPExcel2003Export extends PHPExcel2007Export
{
protected function getWriter()
protected function getWriter(): Xlsx
{
$writer = parent::getWriter();
$writer->setOffice2003Compatibility(true);
Expand Down
11 changes: 5 additions & 6 deletions Grid/Export/PHPExcel2007Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@

namespace APY\DataGridBundle\Grid\Export;

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
* PHPExcel 2007 Export.
*/
class PHPExcel2007Export extends PHPExcel5Export
class PHPExcel2007Export extends PHPExcelExport
{
protected $fileExtension = 'xlsx';

protected $mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

protected function getWriter()
protected function getWriter(): Xlsx
{
$writer = new \PHPExcel_Writer_Excel2007($this->objPHPExcel);
$writer->setPreCalculateFormulas(false);

return $writer;
return new Xlsx($this->objPHPExcel);
}
}
44 changes: 5 additions & 39 deletions Grid/Export/PHPExcel5Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,21 @@

namespace APY\DataGridBundle\Grid\Export;

use APY\DataGridBundle\Grid\Grid;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

/**
* PHPExcel 5 Export (97-2003) (.xls)
* 52 columns maximum.
*/
class PHPExcel5Export extends Export
class PHPExcel5Export extends PHPExcelExport
{
protected $fileExtension = 'xls';

protected $mimeType = 'application/vnd.ms-excel';

public $objPHPExcel;

public function __construct($tilte, $fileName = 'export', $params = [], $charset = 'UTF-8')
{
$this->objPHPExcel = new \PHPExcel();

parent::__construct($tilte, $fileName, $params, $charset);
}

public function computeData(Grid $grid)
{
$data = $this->getFlatGridData($grid);

$row = 1;
foreach ($data as $line) {
$column = 'A';
foreach ($line as $cell) {
$this->objPHPExcel->getActiveSheet()->SetCellValue($column . $row, $cell);

++$column;
}
++$row;
}

$objWriter = $this->getWriter();

ob_start();

$objWriter->save('php://output');

$this->content = ob_get_contents();

ob_end_clean();
}

protected function getWriter()
protected function getWriter(): IWriter
{
return new \PHPExcel_Writer_Excel5($this->objPHPExcel);
return new Xls($this->objPHPExcel);
}
}
69 changes: 69 additions & 0 deletions Grid/Export/PHPExcelExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the DataGridBundle.
*
* (c) Abhoryo <[email protected]>
* (c) Stanislav Turza
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace APY\DataGridBundle\Grid\Export;

use APY\DataGridBundle\Grid\Grid;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

/**
* Excel
*/
class PHPExcelExport extends Export
{
protected $fileExtension = 'xls';

protected $mimeType = 'application/vnd.ms-excel';

protected Spreadsheet $objPHPExcel;

public function __construct($title, $fileName = 'export', $params = [], $charset = 'UTF-8', $role = null)
{
parent::__construct($title, $fileName, $params, $charset, $role);
$this->objPHPExcel = new Spreadsheet;
}

public function computeData(Grid $grid): void
{

$data = $this->getFlatGridData($grid);

$row = 1;
foreach ($data as $line) {
$column = 'A';
foreach ($line as $cell) {
$this->objPHPExcel->getActiveSheet()->SetCellValue($column . $row, $cell);

++$column;
}
++$row;
}

$objWriter = $this->getWriter();

ob_start();

$objWriter->save('php://output');

$this->content = ob_get_contents();

ob_end_clean();
}

protected function getWriter(): IWriter
{
return new Xls($this->objPHPExcel);
}
}
28 changes: 8 additions & 20 deletions Grid/Export/PHPExcelHTMLExport.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
<?php

/*
* This file is part of the DataGridBundle.
*
* (c) Abhoryo <[email protected]>
* (c) Stanislav Turza
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace APY\DataGridBundle\Grid\Export;

/**
* PHPExcel HTML Export.
*/
class PHPExcelHTMLExport extends PHPExcel5Export
{
protected $fileExtension = 'html';
use PhpOffice\PhpSpreadsheet\Writer\Html;

protected $mimeType = 'text/html';
class PHPExcelHTMLExport extends PHPExcelExport
{
protected $fileExtension = "html";
protected $mimeType = "text/html";

protected function getWriter()
protected function getWriter(): Html
{
$writer = new \PHPExcel_Writer_HTML($this->objPHPExcel);
$writer = new Html($this->objPHPExcel);
$writer->setPreCalculateFormulas(false);

return $writer;
}
}
}
17 changes: 17 additions & 0 deletions Grid/Export/PHPExcelMPDFExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace APY\DataGridBundle\Grid\Export;


use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;

class PHPExcelMPDFExport extends PHPExcelPDFExport
{
protected function getWriter(): Mpdf
{
$writer = new Mpdf($this->objPHPExcel);
$writer->setPreCalculateFormulas(false);

return $writer;
}
}
22 changes: 2 additions & 20 deletions Grid/Export/PHPExcelPDFExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,11 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace APY\DataGridBundle\Grid\Export;

/**
* PHPExcel PDF Export.
*/
class PHPExcelPDFExport extends PHPExcel5Export
abstract class PHPExcelPDFExport extends PHPExcelExport
{
protected $fileExtension = 'pdf';

protected $mimeType = 'application/pdf';

protected function getWriter()
{
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setScale(50);
$writer = new \PHPExcel_Writer_PDF($this->objPHPExcel);
$writer->setPreCalculateFormulas(false);
//$writer->setSheetIndex(0);
//$writer->setPaperSize("A4");
$writer->writeAllSheets();

return $writer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Mime type = `application/pdf`
```php
<?php
...
use APY\DataGridBundle\Grid\Export\PHPExcelPDFExport;
use APY\DataGridBundle\Grid\Export\PHPExcelMPDFExport;
...
$grid->setSource($source);

$grid->addExport(new PHPExcelPDFExport($title, $fileName, $params, $charset, $role));
$grid->addExport(new PHPExcelMPDFExport($title, $fileName, $params, $charset, $role));
...
```

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
"doctrine/mongodb-odm": "^2.2",
"rector/rector": "^0.12.13",
"dg/bypass-finals": "^1.3",
"symfony/security-bundle": "^3.0 || ^4.0 || ^5.0",
"symfony/twig-bundle": "^3.0 || ^4.0 || ^5.0",
"doctrine/doctrine-bundle": "^2.5"
"doctrine/doctrine-bundle": "^2.5",
"phpoffice/phpspreadsheet": "^1.22",
"mpdf/mpdf": "^8.0"
},
"suggest": {
"ext-intl": "Translate the grid",
"ext-mbstring": "Convert your data with the right charset",
"PHPExcel": "Export the grid (Excel, HTML or PDF)",
"phpoffice/phpspreadsheet": "Export the grid (Excel, HTML or PDF)",
"doctrine/orm": "If you want to use Entity as source, please require doctrine/orm",
"doctrine/mongodb-odm": "If you want to use Document as source, please require doctrine/mongodb-odm",
"jms/translation-bundle": "If you want to use translations"
Expand Down