Skip to content

Commit c3125ab

Browse files
authored
Use phpoffice/phpspreadsheet for Excel Export (#1105)
* Use phpoffice/phpspreadsheet for Excel Export * ~ rename class
1 parent 6cc1a94 commit c3125ab

9 files changed

Lines changed: 115 additions & 90 deletions

File tree

Grid/Export/PHPExcel2003Export.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
namespace APY\DataGridBundle\Grid\Export;
1414

15+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
16+
1517
/**
1618
* PHPExcel_Excel 2003 Export (.xlsx).
1719
*/
1820
class PHPExcel2003Export extends PHPExcel2007Export
1921
{
20-
protected function getWriter()
22+
protected function getWriter(): Xlsx
2123
{
2224
$writer = parent::getWriter();
2325
$writer->setOffice2003Compatibility(true);

Grid/Export/PHPExcel2007Export.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212

1313
namespace APY\DataGridBundle\Grid\Export;
1414

15+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
16+
1517
/**
1618
* PHPExcel 2007 Export.
1719
*/
18-
class PHPExcel2007Export extends PHPExcel5Export
20+
class PHPExcel2007Export extends PHPExcelExport
1921
{
2022
protected $fileExtension = 'xlsx';
2123

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

24-
protected function getWriter()
26+
protected function getWriter(): Xlsx
2527
{
26-
$writer = new \PHPExcel_Writer_Excel2007($this->objPHPExcel);
27-
$writer->setPreCalculateFormulas(false);
28-
29-
return $writer;
28+
return new Xlsx($this->objPHPExcel);
3029
}
3130
}

Grid/Export/PHPExcel5Export.php

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,21 @@
1212

1313
namespace APY\DataGridBundle\Grid\Export;
1414

15-
use APY\DataGridBundle\Grid\Grid;
15+
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
16+
use PhpOffice\PhpSpreadsheet\Writer\Xls;
1617

1718
/**
1819
* PHPExcel 5 Export (97-2003) (.xls)
1920
* 52 columns maximum.
2021
*/
21-
class PHPExcel5Export extends Export
22+
class PHPExcel5Export extends PHPExcelExport
2223
{
2324
protected $fileExtension = 'xls';
2425

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

27-
public $objPHPExcel;
28-
29-
public function __construct($tilte, $fileName = 'export', $params = [], $charset = 'UTF-8')
30-
{
31-
$this->objPHPExcel = new \PHPExcel();
32-
33-
parent::__construct($tilte, $fileName, $params, $charset);
34-
}
35-
36-
public function computeData(Grid $grid)
37-
{
38-
$data = $this->getFlatGridData($grid);
39-
40-
$row = 1;
41-
foreach ($data as $line) {
42-
$column = 'A';
43-
foreach ($line as $cell) {
44-
$this->objPHPExcel->getActiveSheet()->SetCellValue($column . $row, $cell);
45-
46-
++$column;
47-
}
48-
++$row;
49-
}
50-
51-
$objWriter = $this->getWriter();
52-
53-
ob_start();
54-
55-
$objWriter->save('php://output');
56-
57-
$this->content = ob_get_contents();
58-
59-
ob_end_clean();
60-
}
61-
62-
protected function getWriter()
28+
protected function getWriter(): IWriter
6329
{
64-
return new \PHPExcel_Writer_Excel5($this->objPHPExcel);
30+
return new Xls($this->objPHPExcel);
6531
}
6632
}

Grid/Export/PHPExcelExport.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DataGridBundle.
5+
*
6+
* (c) Abhoryo <abhoryo@free.fr>
7+
* (c) Stanislav Turza
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace APY\DataGridBundle\Grid\Export;
14+
15+
use APY\DataGridBundle\Grid\Grid;
16+
17+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
18+
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
19+
use PhpOffice\PhpSpreadsheet\Writer\Xls;
20+
21+
/**
22+
* Excel
23+
*/
24+
class PHPExcelExport extends Export
25+
{
26+
protected $fileExtension = 'xls';
27+
28+
protected $mimeType = 'application/vnd.ms-excel';
29+
30+
protected Spreadsheet $objPHPExcel;
31+
32+
public function __construct($title, $fileName = 'export', $params = [], $charset = 'UTF-8', $role = null)
33+
{
34+
parent::__construct($title, $fileName, $params, $charset, $role);
35+
$this->objPHPExcel = new Spreadsheet;
36+
}
37+
38+
public function computeData(Grid $grid): void
39+
{
40+
41+
$data = $this->getFlatGridData($grid);
42+
43+
$row = 1;
44+
foreach ($data as $line) {
45+
$column = 'A';
46+
foreach ($line as $cell) {
47+
$this->objPHPExcel->getActiveSheet()->SetCellValue($column . $row, $cell);
48+
49+
++$column;
50+
}
51+
++$row;
52+
}
53+
54+
$objWriter = $this->getWriter();
55+
56+
ob_start();
57+
58+
$objWriter->save('php://output');
59+
60+
$this->content = ob_get_contents();
61+
62+
ob_end_clean();
63+
}
64+
65+
protected function getWriter(): IWriter
66+
{
67+
return new Xls($this->objPHPExcel);
68+
}
69+
}

Grid/Export/PHPExcelHTMLExport.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
<?php
22

3-
/*
4-
* This file is part of the DataGridBundle.
5-
*
6-
* (c) Abhoryo <abhoryo@free.fr>
7-
* (c) Stanislav Turza
8-
*
9-
* For the full copyright and license information, please view the LICENSE
10-
* file that was distributed with this source code.
11-
*/
12-
133
namespace APY\DataGridBundle\Grid\Export;
144

15-
/**
16-
* PHPExcel HTML Export.
17-
*/
18-
class PHPExcelHTMLExport extends PHPExcel5Export
19-
{
20-
protected $fileExtension = 'html';
5+
use PhpOffice\PhpSpreadsheet\Writer\Html;
216

22-
protected $mimeType = 'text/html';
7+
class PHPExcelHTMLExport extends PHPExcelExport
8+
{
9+
protected $fileExtension = "html";
10+
protected $mimeType = "text/html";
2311

24-
protected function getWriter()
12+
protected function getWriter(): Html
2513
{
26-
$writer = new \PHPExcel_Writer_HTML($this->objPHPExcel);
14+
$writer = new Html($this->objPHPExcel);
2715
$writer->setPreCalculateFormulas(false);
2816

2917
return $writer;
3018
}
31-
}
19+
}

Grid/Export/PHPExcelMPDFExport.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace APY\DataGridBundle\Grid\Export;
4+
5+
6+
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
7+
8+
class PHPExcelMPDFExport extends PHPExcelPDFExport
9+
{
10+
protected function getWriter(): Mpdf
11+
{
12+
$writer = new Mpdf($this->objPHPExcel);
13+
$writer->setPreCalculateFormulas(false);
14+
15+
return $writer;
16+
}
17+
}

Grid/Export/PHPExcelPDFExport.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,11 @@
99
* For the full copyright and license information, please view the LICENSE
1010
* file that was distributed with this source code.
1111
*/
12-
1312
namespace APY\DataGridBundle\Grid\Export;
1413

15-
/**
16-
* PHPExcel PDF Export.
17-
*/
18-
class PHPExcelPDFExport extends PHPExcel5Export
14+
abstract class PHPExcelPDFExport extends PHPExcelExport
1915
{
2016
protected $fileExtension = 'pdf';
2117

2218
protected $mimeType = 'application/pdf';
23-
24-
protected function getWriter()
25-
{
26-
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
27-
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
28-
//$this->objPHPExcel->getActiveSheet()->getPageSetup()->setScale(50);
29-
$writer = new \PHPExcel_Writer_PDF($this->objPHPExcel);
30-
$writer->setPreCalculateFormulas(false);
31-
//$writer->setSheetIndex(0);
32-
//$writer->setPaperSize("A4");
33-
$writer->writeAllSheets();
34-
35-
return $writer;
36-
}
37-
}
19+
}

Resources/doc/export/library-dependent_exports/PHPExcel/PHPExcel_PDF_export.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Mime type = `application/pdf`
1010
```php
1111
<?php
1212
...
13-
use APY\DataGridBundle\Grid\Export\PHPExcelPDFExport;
13+
use APY\DataGridBundle\Grid\Export\PHPExcelMPDFExport;
1414
...
1515
$grid->setSource($source);
1616

17-
$grid->addExport(new PHPExcelPDFExport($title, $fileName, $params, $charset, $role));
17+
$grid->addExport(new PHPExcelMPDFExport($title, $fileName, $params, $charset, $role));
1818
...
1919
```
2020

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
"doctrine/mongodb-odm": "^2.2",
4949
"rector/rector": "^0.12.13",
5050
"dg/bypass-finals": "^1.3",
51-
"doctrine/doctrine-bundle": "^2.5"
51+
"doctrine/doctrine-bundle": "^2.5",
52+
"phpoffice/phpspreadsheet": "^1.22",
53+
"mpdf/mpdf": "^8.0"
5254
},
5355
"suggest": {
5456
"ext-intl": "Translate the grid",
5557
"ext-mbstring": "Convert your data with the right charset",
56-
"PHPExcel": "Export the grid (Excel, HTML or PDF)",
58+
"phpoffice/phpspreadsheet": "Export the grid (Excel, HTML or PDF)",
5759
"doctrine/orm": "If you want to use Entity as source, please require doctrine/orm",
5860
"doctrine/mongodb-odm": "If you want to use Document as source, please require doctrine/mongodb-odm",
5961
"jms/translation-bundle": "If you want to use translations"

0 commit comments

Comments
 (0)