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