|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onurb\Bundle\ExcelBundle\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Helper\Html; |
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
|
8
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\IReader; |
|
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
10
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
|
11
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Factory for Spreadsheet objects |
|
15
|
|
|
* |
|
16
|
|
|
* Factory inspired by the Liuggio\ExcelBundle\Factory, migrated for phpSpreadSheet Usage |
|
17
|
|
|
* (PhpExcel used by liuggio is abandoned) |
|
18
|
|
|
* |
|
19
|
|
|
* @package Onurb\Bundle\ExcelBundle |
|
20
|
|
|
*/ |
|
21
|
|
|
class ExcelFactory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var CompatibilityHelper |
|
25
|
|
|
*/ |
|
26
|
|
|
private $compatibilytyHelper; |
|
27
|
|
|
|
|
28
|
19 |
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
19 |
|
$this->compatibilytyHelper = new CompatibilityHelper(); |
|
31
|
19 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Creates an empty Spreadsheet Object if the filename is empty, otherwise loads the file into the object. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $filename |
|
37
|
|
|
* |
|
38
|
|
|
* @return Spreadsheet |
|
39
|
|
|
*/ |
|
40
|
11 |
|
public function createSpreadsheet($filename = null) |
|
41
|
|
|
{ |
|
42
|
11 |
|
return (null === $filename) ? new Spreadsheet() : IOFactory::load($filename); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a worksheet drawing |
|
47
|
|
|
* @return Drawing |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function createSpreadsheetWorksheetDrawing() |
|
50
|
|
|
{ |
|
51
|
3 |
|
return new Drawing(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a reader |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $type |
|
58
|
|
|
* |
|
59
|
|
|
* @return IReader |
|
60
|
|
|
*/ |
|
61
|
5 |
|
public function createReader($type = 'Xlsx') |
|
62
|
|
|
{ |
|
63
|
5 |
|
$type = $this->compatibilytyHelper->convertType($type); |
|
64
|
|
|
|
|
65
|
5 |
|
$class = '\\PhpOffice\\PhpSpreadsheet\\Reader\\' . $type; |
|
66
|
|
|
|
|
67
|
5 |
|
if (!class_exists($class)) { |
|
68
|
2 |
|
throw new \InvalidArgumentException( |
|
69
|
2 |
|
'The reader [' . $type . '] does not exist or is not supported by PhpSpreadsheet.' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
return new $class(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create a writer given the PHPExcelObject and the type, |
|
78
|
|
|
* the type could be one of PHPExcel_IOFactory::$_autoResolveClasses |
|
79
|
|
|
* |
|
80
|
|
|
* @param Spreadsheet $spreadSheetObject |
|
81
|
|
|
* @param string $type |
|
82
|
|
|
* |
|
83
|
|
|
* @return IWriter |
|
84
|
|
|
*/ |
|
85
|
10 |
|
public function createWriter(Spreadsheet $spreadSheetObject, $type = 'Xlsx') |
|
86
|
|
|
{ |
|
87
|
10 |
|
$type = $this->compatibilytyHelper->convertType($type); |
|
88
|
|
|
|
|
89
|
10 |
|
return IOFactory::createWriter($spreadSheetObject, $type); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Stream the file as Response. |
|
94
|
|
|
* |
|
95
|
|
|
* @param IWriter $writer |
|
96
|
|
|
* @param int $status |
|
97
|
|
|
* @param array $headers |
|
98
|
|
|
* |
|
99
|
|
|
* @return StreamedResponse |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function createStreamedResponse(IWriter $writer, $status = 200, $headers = array()) |
|
102
|
|
|
{ |
|
103
|
3 |
|
return new StreamedResponse( |
|
104
|
3 |
|
function () use ($writer) { |
|
105
|
3 |
|
$writer->save('php://output'); |
|
106
|
3 |
|
}, |
|
107
|
3 |
|
$status, |
|
108
|
3 |
|
$headers |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Create a PHPExcel Helper HTML Object |
|
114
|
|
|
* |
|
115
|
|
|
* @return Html |
|
116
|
|
|
*/ |
|
117
|
10 |
|
public function createHelperHTML() |
|
118
|
|
|
{ |
|
119
|
10 |
|
return new Html(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|