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+ }
0 commit comments