From 5a0e09058f618a5897fa7ed3169182273429186c Mon Sep 17 00:00:00 2001 From: Florian Meyer Date: Mon, 12 Mar 2018 15:27:52 +0100 Subject: [PATCH] Migrated from PHPExcel to PhpSpreadsheet --- composer.json | 4 +-- .../ExcelCreator/ConfiguredExcel.php | 7 +++-- .../ExcelCreator/ConfiguredSheet.php | 29 +++++++++++-------- src/StingerSoft/ExcelCreator/Helper.php | 5 ++-- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 5364744..df4f07d 100644 --- a/composer.json +++ b/composer.json @@ -29,9 +29,9 @@ }, "license" : "Apache-2.0", "require" : { - "php" : ">=5.5", + "php": "^5.6|^7.0", "doctrine/collections" : "~1.2", - "phpoffice/phpexcel" : "^1.8.1|^1.9.0", + "phpoffice/phpspreadsheet" : "^1.2.0", "stinger-soft/php-commons" : "~1.0", "symfony/property-access" : "^2.8|^3.0", "symfony/translation" : "^2.8|^3.0" diff --git a/src/StingerSoft/ExcelCreator/ConfiguredExcel.php b/src/StingerSoft/ExcelCreator/ConfiguredExcel.php index b73e768..27fba77 100644 --- a/src/StingerSoft/ExcelCreator/ConfiguredExcel.php +++ b/src/StingerSoft/ExcelCreator/ConfiguredExcel.php @@ -12,6 +12,7 @@ namespace StingerSoft\ExcelCreator; use Doctrine\Common\Collections\ArrayCollection; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use Symfony\Component\Translation\TranslatorInterface; /** @@ -31,7 +32,7 @@ class ConfiguredExcel { /** * The underyling excel file of PHPExcel * - * @var \PHPExcel + * @var Spreadsheet */ protected $phpExcel; @@ -39,7 +40,7 @@ class ConfiguredExcel { * Default contructor */ public function __construct(TranslatorInterface $translator = null) { - $this->phpExcel = new \PHPExcel(); + $this->phpExcel = new Spreadsheet(); $this->sheets = new ArrayCollection(); $this->translator = $translator; } @@ -131,7 +132,7 @@ public function setCompany($company) { /** * Returns the underyling PHPExcel object * - * @return \PHPExcel The underyling PHPExcel object + * @return Spreadsheet The underyling PHPExcel object */ public function getPhpExcel() { return $this->phpExcel; diff --git a/src/StingerSoft/ExcelCreator/ConfiguredSheet.php b/src/StingerSoft/ExcelCreator/ConfiguredSheet.php index eb3fe6a..429e7dc 100644 --- a/src/StingerSoft/ExcelCreator/ConfiguredSheet.php +++ b/src/StingerSoft/ExcelCreator/ConfiguredSheet.php @@ -12,6 +12,11 @@ namespace StingerSoft\ExcelCreator; use Doctrine\Common\Collections\ArrayCollection; +use PhpOffice\PhpSpreadsheet\Cell\Cell; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\Style\Alignment; +use PhpOffice\PhpSpreadsheet\Style\Fill; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\Translation\TranslatorInterface; @@ -104,7 +109,7 @@ class ConfiguredSheet { /** * The PHP worksheet attached to this object * - * @var \PHPExcel_Worksheet + * @var Worksheet */ protected $sheet = null; @@ -122,12 +127,12 @@ class ConfiguredSheet { * * @param ConfiguredExcel $excel * The parent excel file - * @param \PHPExcel_Worksheet $sheet + * @param Worksheet $sheet * The underlying PHP Excel worksheet * @param TranslatorInterface $translator * Translator to support translation of bindings */ - public function __construct(ConfiguredExcel $excel, \PHPExcel_Worksheet $sheet, TranslatorInterface $translator = null) { + public function __construct(ConfiguredExcel $excel, Worksheet $sheet, TranslatorInterface $translator = null) { $this->bindings = new ArrayCollection(); $this->accessor = PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidIndex()->getPropertyAccessor(); $this->excel = $excel; @@ -188,7 +193,7 @@ public function applyData($startColumn = 0, $headerRow = 1) { * The row to start rendering */ protected function renderHeaderRow($startColumn = 0, $headerRow = 1) { - $this->sheet->getStyle(\PHPExcel_Cell::stringFromColumnIndex($startColumn) . $headerRow . ':' . \PHPExcel_Cell::stringFromColumnIndex($startColumn + $this->bindings->count() - 1) . $headerRow)->applyFromArray($this->getDefaultHeaderStyling()); + $this->sheet->getStyle(Coordinate::stringFromColumnIndex($startColumn) . $headerRow . ':' . Coordinate::stringFromColumnIndex($startColumn + $this->bindings->count() - 1) . $headerRow)->applyFromArray($this->getDefaultHeaderStyling()); foreach($this->bindings as $binding) { $cell = $this->sheet->getCellByColumnAndRow($startColumn, $headerRow); $cell->setValue($this->decodeHtmlEntity($this->translate($binding->getLabel(), $binding->getLabelTranslationDomain()))); @@ -243,11 +248,11 @@ protected function renderDataRows($startColumn = 0, $headerRow = 1) { * * Renders a single data cell * - * @param \PHPExcel_Cell $cell + * @param Cell $cell * @param object|array $item * @param ColumnBinding $binding */ - protected function renderDataCell(\PHPExcel_Cell &$cell, $item, ColumnBinding $binding, array $extraData) { + protected function renderDataCell(Cell $cell, $item, ColumnBinding $binding, array $extraData) { $value = $this->getPropertyFromObject($item, $binding, $binding->getBinding(), '', $extraData); $styling = $this->getPropertyFromObject($item, $binding, $binding->getDataStyling(), null, $extraData); @@ -339,7 +344,7 @@ protected function applyTableStyling($startColumn = 0, $headerRow = 1) { // Header filterable $lastColumn = $this->sheet->getHighestDataColumn(); $lastRow = $this->sheet->getHighestDataRow(); - $this->sheet->setAutoFilter(\PHPExcel_Cell::stringFromColumnIndex($startColumn) . $headerRow . ':' . $lastColumn . $lastRow); + $this->sheet->setAutoFilter(Coordinate::stringFromColumnIndex($startColumn) . $headerRow . ':' . $lastColumn . $lastRow); // $this->sheet->setShowSummaryBelow(false); @@ -381,7 +386,7 @@ protected function getDefaultDataStyling($fontColor, $bgColor) { } if($bgColor) { $styling['fill'] = array( - 'type' => \PHPExcel_Style_Fill::FILL_SOLID, + 'type' => Fill::FILL_SOLID, 'color' => array( 'rgb' => $bgColor ) @@ -406,15 +411,15 @@ protected function getDefaultHeaderStyling($fontColor = null, $bgColor = null) { 'bold' => true ), 'fill' => array( - 'type' => \PHPExcel_Style_Fill::FILL_SOLID, + 'type' => Fill::FILL_SOLID, 'color' => array( 'rgb' => $bgColor ?: $this->defaultHeaderBackgroundColor ) ), 'alignment' => array( 'wrap' => true, - 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER, - 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER ) ); } @@ -431,7 +436,7 @@ public function setExtraData($extraData) { /** * Returns the underlying PHP Excel Sheet object * - * @return \PHPExcel_Worksheet + * @return Worksheet */ public function getSheet() { return $this->sheet; diff --git a/src/StingerSoft/ExcelCreator/Helper.php b/src/StingerSoft/ExcelCreator/Helper.php index ebd88e6..46e4099 100644 --- a/src/StingerSoft/ExcelCreator/Helper.php +++ b/src/StingerSoft/ExcelCreator/Helper.php @@ -11,6 +11,7 @@ */ namespace StingerSoft\ExcelCreator; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\Translation\TranslatorInterface; /** @@ -61,10 +62,10 @@ protected function translate($key, $domain) { /** * Sets the title (escaped and shortened) on the given sheet. * - * @param \PHPExcel_Worksheet $sheet + * @param Worksheet $sheet * @param string $title */ - protected function setSheetTitle(\PHPExcel_Worksheet $sheet, $title) { + protected function setSheetTitle(Worksheet $sheet, $title) { $sheet->setTitle(\substr(\str_replace($sheet::getInvalidCharacters(), '_', $title), 0, 31)); } } \ No newline at end of file