1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSpreadsheet\Writer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* PhpSpreadsheet\Writer\Excel2007 |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2006 - 2015 PhpSpreadsheet |
9
|
|
|
* |
10
|
|
|
* This library is free software; you can redistribute it and/or |
11
|
|
|
* modify it under the terms of the GNU Lesser General Public |
12
|
|
|
* License as published by the Free Software Foundation; either |
13
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This library is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18
|
|
|
* Lesser General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Lesser General Public |
21
|
|
|
* License along with this library; if not, write to the Free Software |
22
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23
|
|
|
* |
24
|
|
|
* @category PhpSpreadsheet |
25
|
|
|
* @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
26
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
27
|
|
|
* @version ##VERSION##, ##DATE## |
28
|
|
|
*/ |
29
|
|
|
class Excel2007 extends BaseWriter implements IWriter |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Office2003 compatibility |
33
|
|
|
* |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
private $office2003compatibility = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Private writer parts |
40
|
|
|
* |
41
|
|
|
* @var Excel2007\WriterPart[] |
42
|
|
|
*/ |
43
|
|
|
private $writerParts = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Private Spreadsheet |
47
|
|
|
* |
48
|
|
|
* @var \PhpSpreadsheet\Spreadsheet |
49
|
|
|
*/ |
50
|
|
|
private $spreadSheet; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Private string table |
54
|
|
|
* |
55
|
|
|
* @var string[] |
56
|
|
|
*/ |
57
|
|
|
private $stringTable = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Private unique \PhpSpreadsheet\Style\Conditional HashTable |
61
|
|
|
* |
62
|
|
|
* @var \PhpSpreadsheet\HashTable |
63
|
|
|
*/ |
64
|
|
|
private $stylesConditionalHashTable; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Private unique \PhpSpreadsheet\Style HashTable |
68
|
|
|
* |
69
|
|
|
* @var \PhpSpreadsheet\HashTable |
70
|
|
|
*/ |
71
|
|
|
private $styleHashTable; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Private unique \PhpSpreadsheet\Style\Fill HashTable |
75
|
|
|
* |
76
|
|
|
* @var \PhpSpreadsheet\HashTable |
77
|
|
|
*/ |
78
|
|
|
private $fillHashTable; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Private unique \PhpSpreadsheet\Style\Font HashTable |
82
|
|
|
* |
83
|
|
|
* @var \PhpSpreadsheet\HashTable |
84
|
|
|
*/ |
85
|
|
|
private $fontHashTable; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Private unique \PhpSpreadsheet\Style\Borders HashTable |
89
|
|
|
* |
90
|
|
|
* @var \PhpSpreadsheet\HashTable |
91
|
|
|
*/ |
92
|
|
|
private $bordersHashTable ; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Private unique \PhpSpreadsheet\Style\NumberFormat HashTable |
96
|
|
|
* |
97
|
|
|
* @var \PhpSpreadsheet\HashTable |
98
|
|
|
*/ |
99
|
|
|
private $numFmtHashTable; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Private unique \PhpSpreadsheet\Worksheet\BaseDrawing HashTable |
103
|
|
|
* |
104
|
|
|
* @var \PhpSpreadsheet\HashTable |
105
|
|
|
*/ |
106
|
|
|
private $drawingHashTable; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create a new Excel2007 Writer |
110
|
|
|
* |
111
|
|
|
* @param \PhpSpreadsheet\SpreadSheet $spreadsheet |
112
|
|
|
*/ |
113
|
|
|
public function __construct(\PhpSpreadsheet\Spreadsheet $spreadsheet = null) |
114
|
|
|
{ |
115
|
|
|
// Assign PhpSpreadsheet |
116
|
|
|
$this->setPhpSpreadsheet($spreadsheet); |
117
|
|
|
|
118
|
|
|
$writerPartsArray = [ |
119
|
|
|
'stringtable' => '\\PhpSpreadsheet\\Writer\\Excel2007\\StringTable', |
120
|
|
|
'contenttypes' => '\\PhpSpreadsheet\\Writer\\Excel2007\\ContentTypes', |
121
|
|
|
'docprops' => '\\PhpSpreadsheet\\Writer\\Excel2007\\DocProps', |
122
|
|
|
'rels' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Rels', |
123
|
|
|
'theme' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Theme', |
124
|
|
|
'style' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Style', |
125
|
|
|
'workbook' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Workbook', |
126
|
|
|
'worksheet' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Worksheet', |
127
|
|
|
'drawing' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Drawing', |
128
|
|
|
'comments' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Comments', |
129
|
|
|
'chart' => '\\PhpSpreadsheet\\Writer\\Excel2007\\Chart', |
130
|
|
|
'relsvba' => '\\PhpSpreadsheet\\Writer\\Excel2007\\RelsVBA', |
131
|
|
|
'relsribbonobjects' => '\\PhpSpreadsheet\\Writer\\Excel2007\\RelsRibbon' |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
// Initialise writer parts |
135
|
|
|
// and Assign their parent IWriters |
136
|
|
|
foreach ($writerPartsArray as $writer => $class) { |
137
|
|
|
$this->writerParts[$writer] = new $class($this); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$hashTablesArray = array( 'stylesConditionalHashTable', 'fillHashTable', 'fontHashTable', |
141
|
|
|
'bordersHashTable', 'numFmtHashTable', 'drawingHashTable', |
142
|
|
|
'styleHashTable' |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
// Set HashTable variables |
146
|
|
|
foreach ($hashTablesArray as $tableName) { |
147
|
|
|
$this->$tableName = new \PhpSpreadsheet\HashTable(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get writer part |
153
|
|
|
* |
154
|
|
|
* @param string $pPartName Writer part name |
155
|
|
|
* @return \PhpSpreadsheet\Writer\Excel2007\WriterPart |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function getWriterPart($pPartName = '') |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) { |
160
|
|
|
return $this->writerParts[strtolower($pPartName)]; |
161
|
|
|
} else { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Save PhpSpreadsheet to file |
168
|
|
|
* |
169
|
|
|
* @param string $pFilename |
170
|
|
|
* @throws \PhpSpreadsheet\Writer\Exception |
171
|
|
|
*/ |
172
|
|
|
public function save($pFilename = null) |
173
|
|
|
{ |
174
|
|
|
if ($this->spreadSheet !== null) { |
175
|
|
|
// garbage collect |
176
|
|
|
$this->spreadSheet->garbageCollect(); |
177
|
|
|
|
178
|
|
|
// If $pFilename is php://output or php://stdout, make it a temporary file... |
179
|
|
|
$originalFilename = $pFilename; |
180
|
|
View Code Duplication |
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
|
|
|
|
181
|
|
|
$pFilename = @tempnam(\PhpSpreadsheet\Shared\File::sysGetTempDir(), 'phpxltmp'); |
182
|
|
|
if ($pFilename == '') { |
183
|
|
|
$pFilename = $originalFilename; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$saveDebugLog = \PhpSpreadsheet\Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog(); |
188
|
|
|
\PhpSpreadsheet\Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false); |
189
|
|
|
$saveDateReturnType = \PhpSpreadsheet\Calculation\Functions::getReturnDateType(); |
190
|
|
|
\PhpSpreadsheet\Calculation\Functions::setReturnDateType(\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL); |
191
|
|
|
|
192
|
|
|
// Create string lookup table |
193
|
|
|
$this->stringTable = array(); |
194
|
|
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
195
|
|
|
$this->stringTable = $this->getWriterPart('StringTable')->createStringTable($this->spreadSheet->getSheet($i), $this->stringTable); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Create styles dictionaries |
199
|
|
|
$this->styleHashTable->addFromSource($this->getWriterPart('Style')->allStyles($this->spreadSheet)); |
|
|
|
|
200
|
|
|
$this->stylesConditionalHashTable->addFromSource($this->getWriterPart('Style')->allConditionalStyles($this->spreadSheet)); |
|
|
|
|
201
|
|
|
$this->fillHashTable->addFromSource($this->getWriterPart('Style')->allFills($this->spreadSheet)); |
|
|
|
|
202
|
|
|
$this->fontHashTable->addFromSource($this->getWriterPart('Style')->allFonts($this->spreadSheet)); |
|
|
|
|
203
|
|
|
$this->bordersHashTable->addFromSource($this->getWriterPart('Style')->allBorders($this->spreadSheet)); |
|
|
|
|
204
|
|
|
$this->numFmtHashTable->addFromSource($this->getWriterPart('Style')->allNumberFormats($this->spreadSheet)); |
|
|
|
|
205
|
|
|
|
206
|
|
|
// Create drawing dictionary |
207
|
|
|
$this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet)); |
|
|
|
|
208
|
|
|
|
209
|
|
|
// Create new ZIP file and open it for writing |
210
|
|
|
$zipClass = \PhpSpreadsheet\Settings::getZipClass(); |
211
|
|
|
/** @var \ZipArchive $objZip */ |
212
|
|
|
$objZip = new $zipClass(); |
213
|
|
|
|
214
|
|
|
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class |
215
|
|
|
// This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP |
216
|
|
|
$ro = new \ReflectionObject($objZip); |
217
|
|
|
$zipOverWrite = $ro->getConstant('OVERWRITE'); |
218
|
|
|
$zipCreate = $ro->getConstant('CREATE'); |
219
|
|
|
|
220
|
|
|
if (file_exists($pFilename)) { |
221
|
|
|
unlink($pFilename); |
222
|
|
|
} |
223
|
|
|
// Try opening the ZIP file |
224
|
|
View Code Duplication |
if ($objZip->open($pFilename, $zipOverWrite) !== true) { |
|
|
|
|
225
|
|
|
if ($objZip->open($pFilename, $zipCreate) !== true) { |
226
|
|
|
throw new \PhpSpreadsheet\Writer\Exception("Could not open " . $pFilename . " for writing."); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// Add [Content_Types].xml to ZIP file |
231
|
|
|
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts)); |
|
|
|
|
232
|
|
|
|
233
|
|
|
//if hasMacros, add the vbaProject.bin file, Certificate file(if exists) |
234
|
|
|
if ($this->spreadSheet->hasMacros()) { |
235
|
|
|
$macrosCode=$this->spreadSheet->getMacrosCode(); |
236
|
|
|
if (!is_null($macrosCode)) {// we have the code ? |
237
|
|
|
$objZip->addFromString('xl/vbaProject.bin', $macrosCode);//allways in 'xl', allways named vbaProject.bin |
238
|
|
|
if ($this->spreadSheet->hasMacrosCertificate()) {//signed macros ? |
239
|
|
|
// Yes : add the certificate file and the related rels file |
240
|
|
|
$objZip->addFromString('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate()); |
241
|
|
|
$objZip->addFromString('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet)); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels) |
246
|
|
|
if ($this->spreadSheet->hasRibbon()) { |
247
|
|
|
$tmpRibbonTarget=$this->spreadSheet->getRibbonXMLData('target'); |
248
|
|
|
$objZip->addFromString($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data')); |
249
|
|
|
if ($this->spreadSheet->hasRibbonBinObjects()) { |
250
|
|
|
$tmpRootPath=dirname($tmpRibbonTarget).'/'; |
251
|
|
|
$ribbonBinObjects=$this->spreadSheet->getRibbonBinObjects('data');//the files to write |
252
|
|
|
foreach ($ribbonBinObjects as $aPath => $aContent) { |
253
|
|
|
$objZip->addFromString($tmpRootPath.$aPath, $aContent); |
254
|
|
|
} |
255
|
|
|
//the rels for files |
256
|
|
|
$objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet)); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// Add relationships to ZIP file |
261
|
|
|
$objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet)); |
|
|
|
|
262
|
|
|
$objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet)); |
|
|
|
|
263
|
|
|
|
264
|
|
|
// Add document properties to ZIP file |
265
|
|
|
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet)); |
|
|
|
|
266
|
|
|
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet)); |
|
|
|
|
267
|
|
|
$customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet); |
|
|
|
|
268
|
|
|
if ($customPropertiesPart !== null) { |
269
|
|
|
$objZip->addFromString('docProps/custom.xml', $customPropertiesPart); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// Add theme to ZIP file |
273
|
|
|
$objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet)); |
|
|
|
|
274
|
|
|
|
275
|
|
|
// Add string table to ZIP file |
276
|
|
|
$objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable)); |
|
|
|
|
277
|
|
|
|
278
|
|
|
// Add styles to ZIP file |
279
|
|
|
$objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet)); |
|
|
|
|
280
|
|
|
|
281
|
|
|
// Add workbook to ZIP file |
282
|
|
|
$objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas)); |
|
|
|
|
283
|
|
|
|
284
|
|
|
$chartCount = 0; |
285
|
|
|
// Add worksheets |
286
|
|
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
287
|
|
|
$objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts)); |
|
|
|
|
288
|
|
|
if ($this->includeCharts) { |
289
|
|
|
$charts = $this->spreadSheet->getSheet($i)->getChartCollection(); |
290
|
|
|
if (count($charts) > 0) { |
291
|
|
|
foreach ($charts as $chart) { |
292
|
|
|
$objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas)); |
|
|
|
|
293
|
|
|
$chartCount++; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$chartRef1 = $chartRef2 = 0; |
300
|
|
|
// Add worksheet relationships (drawings, ...) |
301
|
|
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
302
|
|
|
// Add relationships |
303
|
|
|
$objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts)); |
|
|
|
|
304
|
|
|
|
305
|
|
|
$drawings = $this->spreadSheet->getSheet($i)->getDrawingCollection(); |
306
|
|
|
$drawingCount = count($drawings); |
307
|
|
|
if ($this->includeCharts) { |
308
|
|
|
$chartCount = $this->spreadSheet->getSheet($i)->getChartCount(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
// Add drawing and image relationship parts |
312
|
|
|
if (($drawingCount > 0) || ($chartCount > 0)) { |
313
|
|
|
// Drawing relationships |
314
|
|
|
$objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts)); |
|
|
|
|
315
|
|
|
|
316
|
|
|
// Drawings |
317
|
|
|
$objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $chartRef2, $this->includeCharts)); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// Add comment relationship parts |
321
|
|
|
if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) { |
322
|
|
|
// VML Comments |
323
|
|
|
$objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i))); |
|
|
|
|
324
|
|
|
|
325
|
|
|
// Comments |
326
|
|
|
$objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i))); |
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Add header/footer relationship parts |
330
|
|
|
if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { |
331
|
|
|
// VML Drawings |
332
|
|
|
$objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i))); |
|
|
|
|
333
|
|
|
|
334
|
|
|
// VML Drawing relationships |
335
|
|
|
$objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i))); |
|
|
|
|
336
|
|
|
|
337
|
|
|
// Media |
338
|
|
|
foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { |
339
|
|
|
$objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath())); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
// Add media |
345
|
|
|
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { |
346
|
|
|
if ($this->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\Drawing) { |
347
|
|
|
$imageContents = null; |
|
|
|
|
348
|
|
|
$imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); |
349
|
|
|
if (strpos($imagePath, 'zip://') !== false) { |
350
|
|
|
$imagePath = substr($imagePath, 6); |
351
|
|
|
$imagePathSplitted = explode('#', $imagePath); |
352
|
|
|
|
353
|
|
|
$zipClass = \PhpSpreadsheet\Settings::getZipClass(); |
354
|
|
|
$imageZip = new $zipClass(); |
355
|
|
|
$imageZip->open($imagePathSplitted[0]); |
356
|
|
|
$imageContents = $imageZip->getFromName($imagePathSplitted[1]); |
357
|
|
|
$imageZip->close(); |
358
|
|
|
unset($imageZip); |
359
|
|
|
} else { |
360
|
|
|
$imageContents = file_get_contents($imagePath); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
364
|
|
|
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\MemoryDrawing) { |
365
|
|
|
ob_start(); |
366
|
|
|
call_user_func( |
367
|
|
|
$this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), |
368
|
|
|
$this->getDrawingHashTable()->getByIndex($i)->getImageResource() |
369
|
|
|
); |
370
|
|
|
$imageContents = ob_get_contents(); |
371
|
|
|
ob_end_clean(); |
372
|
|
|
|
373
|
|
|
$objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveDateReturnType); |
378
|
|
|
\PhpSpreadsheet\Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
379
|
|
|
|
380
|
|
|
// Close file |
381
|
|
|
if ($objZip->close() === false) { |
382
|
|
|
throw new \PhpSpreadsheet\Writer\Exception("Could not close zip file $pFilename."); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
// If a temporary file was used, copy it to the correct file stream |
386
|
|
View Code Duplication |
if ($originalFilename != $pFilename) { |
|
|
|
|
387
|
|
|
if (copy($pFilename, $originalFilename) === false) { |
388
|
|
|
throw new \PhpSpreadsheet\Writer\Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
389
|
|
|
} |
390
|
|
|
@unlink($pFilename); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
} else { |
393
|
|
|
throw new \PhpSpreadsheet\Writer\Exception("PhpSpreadsheet object unassigned."); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Get PhpSpreadsheet object |
399
|
|
|
* |
400
|
|
|
* @return PhpSpreadsheet |
401
|
|
|
* @throws \PhpSpreadsheet\Writer\Exception |
402
|
|
|
*/ |
403
|
|
|
public function getPhpSpreadsheet() |
404
|
|
|
{ |
405
|
|
|
if ($this->spreadSheet !== null) { |
406
|
|
|
return $this->spreadSheet; |
407
|
|
|
} else { |
408
|
|
|
throw new \PhpSpreadsheet\Writer\Exception("No PhpSpreadsheet object assigned."); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Set PhpSpreadsheet object |
414
|
|
|
* |
415
|
|
|
* @param \PhpSpreadsheet\Spreadsheet $spreadsheet PhpSpreadsheet object |
416
|
|
|
* @throws Exception |
417
|
|
|
* @return Excel2007 |
418
|
|
|
*/ |
419
|
|
|
public function setPhpSpreadsheet(\PhpSpreadsheet\Spreadsheet $spreadsheet = null) |
420
|
|
|
{ |
421
|
|
|
$this->spreadSheet = $spreadsheet; |
422
|
|
|
return $this; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Get string table |
427
|
|
|
* |
428
|
|
|
* @return string[] |
429
|
|
|
*/ |
430
|
|
|
public function getStringTable() |
431
|
|
|
{ |
432
|
|
|
return $this->stringTable; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Get \PhpSpreadsheet\Style HashTable |
437
|
|
|
* |
438
|
|
|
* @return \PhpSpreadsheet\HashTable |
439
|
|
|
*/ |
440
|
|
|
public function getStyleHashTable() |
441
|
|
|
{ |
442
|
|
|
return $this->styleHashTable; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Get \PhpSpreadsheet\Style\Conditional HashTable |
447
|
|
|
* |
448
|
|
|
* @return \PhpSpreadsheet\HashTable |
449
|
|
|
*/ |
450
|
|
|
public function getStylesConditionalHashTable() |
451
|
|
|
{ |
452
|
|
|
return $this->stylesConditionalHashTable; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Get \PhpSpreadsheet\Style\Fill HashTable |
457
|
|
|
* |
458
|
|
|
* @return \PhpSpreadsheet\HashTable |
459
|
|
|
*/ |
460
|
|
|
public function getFillHashTable() |
461
|
|
|
{ |
462
|
|
|
return $this->fillHashTable; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Get \PhpSpreadsheet\Style\Font HashTable |
467
|
|
|
* |
468
|
|
|
* @return \PhpSpreadsheet\HashTable |
469
|
|
|
*/ |
470
|
|
|
public function getFontHashTable() |
471
|
|
|
{ |
472
|
|
|
return $this->fontHashTable; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Get \PhpSpreadsheet\Style\Borders HashTable |
477
|
|
|
* |
478
|
|
|
* @return \PhpSpreadsheet\HashTable |
479
|
|
|
*/ |
480
|
|
|
public function getBordersHashTable() |
481
|
|
|
{ |
482
|
|
|
return $this->bordersHashTable; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Get \PhpSpreadsheet\Style\NumberFormat HashTable |
487
|
|
|
* |
488
|
|
|
* @return \PhpSpreadsheet\HashTable |
489
|
|
|
*/ |
490
|
|
|
public function getNumFmtHashTable() |
491
|
|
|
{ |
492
|
|
|
return $this->numFmtHashTable; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Get \PhpSpreadsheet\Worksheet\BaseDrawing HashTable |
497
|
|
|
* |
498
|
|
|
* @return \PhpSpreadsheet\HashTable |
499
|
|
|
*/ |
500
|
|
|
public function getDrawingHashTable() |
501
|
|
|
{ |
502
|
|
|
return $this->drawingHashTable; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Get Office2003 compatibility |
507
|
|
|
* |
508
|
|
|
* @return boolean |
509
|
|
|
*/ |
510
|
|
|
public function getOffice2003Compatibility() |
511
|
|
|
{ |
512
|
|
|
return $this->office2003compatibility; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Set Office2003 compatibility |
517
|
|
|
* |
518
|
|
|
* @param boolean $pValue Office2003 compatibility? |
519
|
|
|
* @return Excel2007 |
520
|
|
|
*/ |
521
|
|
|
public function setOffice2003Compatibility($pValue = false) |
522
|
|
|
{ |
523
|
|
|
$this->office2003compatibility = $pValue; |
524
|
|
|
return $this; |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.