From 2fc6e3e2b91c2a1a91e0320d2a425a3e791ddef0 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Tue, 7 May 2024 17:33:56 +0200 Subject: [PATCH 1/2] Use phpoffice/phpspreadsheet for Excel Export --- Grid/Export/PHPExcel2003Export.php | 4 +- Grid/Export/PHPExcel2007Export.php | 11 +++-- Grid/Export/PHPExcel5Export.php | 44 +++---------------- Grid/Export/PHPExcelExport.php | 69 ++++++++++++++++++++++++++++++ Grid/Export/PHPExcelHTMLExport.php | 28 ++++-------- Grid/Export/PHPExcelMDFExport.php | 17 ++++++++ Grid/Export/PHPExcelPDFExport.php | 22 +--------- composer.json | 7 +-- 8 files changed, 113 insertions(+), 89 deletions(-) create mode 100644 Grid/Export/PHPExcelExport.php create mode 100644 Grid/Export/PHPExcelMDFExport.php diff --git a/Grid/Export/PHPExcel2003Export.php b/Grid/Export/PHPExcel2003Export.php index 2f5d5804..3077dd70 100644 --- a/Grid/Export/PHPExcel2003Export.php +++ b/Grid/Export/PHPExcel2003Export.php @@ -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); diff --git a/Grid/Export/PHPExcel2007Export.php b/Grid/Export/PHPExcel2007Export.php index eafea087..4c212fe3 100644 --- a/Grid/Export/PHPExcel2007Export.php +++ b/Grid/Export/PHPExcel2007Export.php @@ -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); } } diff --git a/Grid/Export/PHPExcel5Export.php b/Grid/Export/PHPExcel5Export.php index 9732be7b..50da00f9 100644 --- a/Grid/Export/PHPExcel5Export.php +++ b/Grid/Export/PHPExcel5Export.php @@ -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); } } diff --git a/Grid/Export/PHPExcelExport.php b/Grid/Export/PHPExcelExport.php new file mode 100644 index 00000000..e972b320 --- /dev/null +++ b/Grid/Export/PHPExcelExport.php @@ -0,0 +1,69 @@ + + * (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); + } +} \ No newline at end of file diff --git a/Grid/Export/PHPExcelHTMLExport.php b/Grid/Export/PHPExcelHTMLExport.php index 911e1810..ab8ebbd0 100644 --- a/Grid/Export/PHPExcelHTMLExport.php +++ b/Grid/Export/PHPExcelHTMLExport.php @@ -1,31 +1,19 @@ - * (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; } -} +} \ No newline at end of file diff --git a/Grid/Export/PHPExcelMDFExport.php b/Grid/Export/PHPExcelMDFExport.php new file mode 100644 index 00000000..b71550e6 --- /dev/null +++ b/Grid/Export/PHPExcelMDFExport.php @@ -0,0 +1,17 @@ +objPHPExcel); + $writer->setPreCalculateFormulas(false); + + return $writer; + } +} \ No newline at end of file diff --git a/Grid/Export/PHPExcelPDFExport.php b/Grid/Export/PHPExcelPDFExport.php index c71017d5..f9fa571c 100644 --- a/Grid/Export/PHPExcelPDFExport.php +++ b/Grid/Export/PHPExcelPDFExport.php @@ -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; - } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index dd0e3f08..85c4835e 100644 --- a/composer.json +++ b/composer.json @@ -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" From f1eded1cb9bc03b8970e31b4454177d12144324b Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Wed, 8 May 2024 10:58:31 +0200 Subject: [PATCH 2/2] ~ rename class --- Grid/Export/{PHPExcelMDFExport.php => PHPExcelMPDFExport.php} | 2 +- .../library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename Grid/Export/{PHPExcelMDFExport.php => PHPExcelMPDFExport.php} (84%) diff --git a/Grid/Export/PHPExcelMDFExport.php b/Grid/Export/PHPExcelMPDFExport.php similarity index 84% rename from Grid/Export/PHPExcelMDFExport.php rename to Grid/Export/PHPExcelMPDFExport.php index b71550e6..60167146 100644 --- a/Grid/Export/PHPExcelMDFExport.php +++ b/Grid/Export/PHPExcelMPDFExport.php @@ -5,7 +5,7 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; -class PHPExcelMDFExport extends PHPExcelPDFExport +class PHPExcelMPDFExport extends PHPExcelPDFExport { protected function getWriter(): Mpdf { diff --git a/Resources/doc/export/library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md b/Resources/doc/export/library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md index a9fbb9a6..82f81746 100644 --- a/Resources/doc/export/library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md +++ b/Resources/doc/export/library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md @@ -10,11 +10,11 @@ Mime type = `application/pdf` ```php setSource($source); -$grid->addExport(new PHPExcelPDFExport($title, $fileName, $params, $charset, $role)); +$grid->addExport(new PHPExcelMPDFExport($title, $fileName, $params, $charset, $role)); ... ```