1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onurb\Bundle\ExcelBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Onurb\Bundle\ExcelBundle\Factory\ExcelFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\IReader; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
12
|
|
|
|
13
|
|
|
class FakeController extends Controller |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return StreamedResponse |
17
|
|
|
*/ |
18
|
|
|
public function streamAction() |
19
|
|
|
{ |
20
|
|
|
/** @var ExcelFactory $factory */ |
21
|
|
|
$factory = $this->get('phpspreadsheet'); |
22
|
|
|
|
23
|
|
|
// create an empty object |
24
|
|
|
$phpExcelObject = $this->createSpreadsheet(); |
25
|
|
|
// create the writer |
26
|
|
|
$writer = $factory->createWriter($phpExcelObject, 'Xls'); |
27
|
|
|
// create the response |
28
|
|
|
$response = $factory->createStreamedResponse($writer); |
29
|
|
|
// adding headers |
30
|
|
|
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8'); |
31
|
|
|
$response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls'); |
32
|
|
|
$response->headers->set('Pragma', 'public'); |
33
|
|
|
$response->headers->set('Cache-Control', 'maxage=1'); |
34
|
|
|
|
35
|
|
|
return $response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return Response |
40
|
|
|
*/ |
41
|
|
|
public function storeAction() |
42
|
|
|
{ |
43
|
|
|
/** @var ExcelFactory $factory */ |
44
|
|
|
$factory = $this->get('phpspreadsheet'); |
45
|
|
|
|
46
|
|
|
// create an empty object |
47
|
|
|
$spreadsheet = $this->createSpreadsheet(); |
48
|
|
|
// create the writer |
49
|
|
|
$writer = $factory->createWriter($spreadsheet, 'Xls'); |
50
|
|
|
$tmpFilename = tempnam(sys_get_temp_dir(), 'xls-'); |
51
|
|
|
$filename = $tmpFilename . '.xls'; |
52
|
|
|
// create filename |
53
|
|
|
$writer->save($filename); |
54
|
|
|
unlink($tmpFilename); |
55
|
|
|
|
56
|
|
|
return new Response($filename, 201); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Response |
61
|
|
|
*/ |
62
|
|
|
public function readAndSaveAction() |
63
|
|
|
{ |
64
|
|
|
/** @var ExcelFactory $factory */ |
65
|
|
|
$factory = $this->get('phpspreadsheet'); |
66
|
|
|
|
67
|
|
|
// create an object from a filename |
68
|
|
|
$spreadsheet = $this->createSpreadsheet(); |
69
|
|
|
// create the writer |
70
|
|
|
$writer = $factory->createWriter($spreadsheet, 'Xls'); |
71
|
|
|
$tmpFilename = tempnam(sys_get_temp_dir(), 'xls-'); |
72
|
|
|
$filename = $tmpFilename . '.xls'; |
73
|
|
|
|
74
|
|
|
// create filename |
75
|
|
|
$writer->save($filename); |
76
|
|
|
unlink($tmpFilename); |
77
|
|
|
|
78
|
|
|
return new Response($filename, 201); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Response |
83
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
84
|
|
|
*/ |
85
|
|
|
public function readerAction() |
86
|
|
|
{ |
87
|
|
|
$filename = $this->container->getParameter('xls_fixture_absolute_path'); |
88
|
|
|
|
89
|
|
|
// load the factory |
90
|
|
|
/** @var \Onurb\Bundle\ExcelBundle\Factory\ExcelFactory $factory */ |
91
|
|
|
$factory = $this->get('phpspreadsheet'); |
92
|
|
|
// create a reader |
93
|
|
|
$reader = $factory->createReader('Xls'); |
94
|
|
|
// check that the file can be read |
95
|
|
|
$canread = $reader->canRead($filename); |
96
|
|
|
// check that an empty temporary file cannot be read |
97
|
|
|
$someFile = tempnam($this->getParameter('kernel.root_dir'), "tmp"); |
98
|
|
|
$cannotread = $reader->canRead($someFile); |
99
|
|
|
unlink($someFile); |
100
|
|
|
|
101
|
|
|
// load the excel file |
102
|
|
|
$spreadsheet = $reader->load($filename); |
103
|
|
|
// read some data |
104
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
105
|
|
|
$hello = $sheet->getCell('A1')->getValue(); |
106
|
|
|
$world = $sheet->getCell('B2')->getValue(); |
107
|
|
|
|
108
|
|
|
return new Response($canread && !$cannotread ? "$hello $world" : 'I should no be able to read this.'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function createSpreadSheetWithDrawingAction() |
112
|
|
|
{ |
113
|
|
|
/** @var ExcelFactory $factory */ |
114
|
|
|
$factory = $this->get('phpspreadsheet'); |
115
|
|
|
$spreadsheet = $this->createSpreadsheet(); |
116
|
|
|
|
117
|
|
|
$writer = $factory->createWriter($spreadsheet, 'Xls'); |
118
|
|
|
|
119
|
|
|
$drawing = $factory->createSpreadsheetWorksheetDrawing(); |
120
|
|
|
|
121
|
|
|
$drawing->setPath(__DIR__ . '/../fixture/doctrine.png') |
122
|
|
|
->setName('Test') |
123
|
|
|
->setDescription('Test drawing object'); |
124
|
|
|
|
125
|
|
|
$drawing->setWorksheet($spreadsheet->getActiveSheet()); |
126
|
|
|
$tmpFilename = tempnam(sys_get_temp_dir(), 'xls-'); |
127
|
|
|
$filename = $tmpFilename . '.xls'; |
128
|
|
|
|
129
|
|
|
$writer->save($filename); |
130
|
|
|
|
131
|
|
|
//clean the tmp file |
132
|
|
|
|
133
|
|
|
unlink($tmpFilename); |
134
|
|
|
|
135
|
|
|
return new Response($filename, 201); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* utility class |
140
|
|
|
* @return Spreadsheet |
141
|
|
|
*/ |
142
|
|
|
private function createSpreadsheet() |
143
|
|
|
{ |
144
|
|
|
/** @var ExcelFactory $factory */ |
145
|
|
|
$factory = $this->get('phpspreadsheet'); |
146
|
|
|
$spreadsheet = $factory->createSpreadsheet(); |
147
|
|
|
|
148
|
|
|
$htmlHelper = $factory->createHelperHTML(); |
149
|
|
|
|
150
|
|
|
$spreadsheet->getProperties()->setCreator("liuggio") |
151
|
|
|
->setLastModifiedBy("Giulio De Donato") |
152
|
|
|
->setTitle("Office 2005 XLSX Test Document") |
153
|
|
|
->setSubject("Office 2005 XLSX Test Document") |
154
|
|
|
->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.") |
155
|
|
|
->setKeywords("office 2005 openxml php") |
156
|
|
|
->setCategory("Test result file"); |
157
|
|
|
$spreadsheet->setActiveSheetIndex(0) |
158
|
|
|
->setCellValue('A1', 'Hello') |
159
|
|
|
->setCellValue('B2', 'world!') |
160
|
|
|
->setCellValue('C3', $htmlHelper->toRichTextObject('<b>In Bold!</b>')); |
161
|
|
|
$spreadsheet->getActiveSheet()->setTitle('Simple'); |
162
|
|
|
// Set active sheet index to the first sheet, so Excel opens this as the first sheet |
163
|
|
|
$spreadsheet->setActiveSheetIndex(0); |
164
|
|
|
|
165
|
|
|
return $spreadsheet; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.