Skip to content

Commit

Permalink
Migrated from PHPExcel to PhpSpreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Viburnum committed Mar 12, 2018
1 parent 27b5c89 commit 5a0e090
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions src/StingerSoft/ExcelCreator/ConfiguredExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace StingerSoft\ExcelCreator;

use Doctrine\Common\Collections\ArrayCollection;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand All @@ -31,15 +32,15 @@ class ConfiguredExcel {
/**
* The underyling excel file of PHPExcel
*
* @var \PHPExcel
* @var Spreadsheet
*/
protected $phpExcel;

/**
* Default contructor
*/
public function __construct(TranslatorInterface $translator = null) {
$this->phpExcel = new \PHPExcel();
$this->phpExcel = new Spreadsheet();
$this->sheets = new ArrayCollection();
$this->translator = $translator;
}
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 17 additions & 12 deletions src/StingerSoft/ExcelCreator/ConfiguredSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +109,7 @@ class ConfiguredSheet {
/**
* The PHP worksheet attached to this object
*
* @var \PHPExcel_Worksheet
* @var Worksheet
*/
protected $sheet = null;

Expand All @@ -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;
Expand Down Expand Up @@ -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())));
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
)
Expand All @@ -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
)
);
}
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/StingerSoft/ExcelCreator/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace StingerSoft\ExcelCreator;

use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 5a0e090

Please sign in to comment.