1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\HashTable; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Borders; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing as WorksheetDrawing; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Chart; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Comments; |
21
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\ContentTypes; |
22
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\DocProps; |
23
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Drawing; |
24
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels; |
25
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\RelsRibbon; |
26
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\RelsVBA; |
27
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\StringTable; |
28
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Style; |
29
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Table; |
30
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Theme; |
31
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Workbook; |
32
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet; |
33
|
|
|
use ZipArchive; |
34
|
|
|
use ZipStream\Exception\OverflowException; |
35
|
|
|
use ZipStream\ZipStream; |
36
|
|
|
|
37
|
|
|
class Xlsx extends BaseWriter |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Office2003 compatibility. |
41
|
|
|
*/ |
42
|
|
|
private bool $office2003compatibility = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Private Spreadsheet. |
46
|
|
|
*/ |
47
|
|
|
private Spreadsheet $spreadSheet; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Private string table. |
51
|
|
|
* |
52
|
|
|
* @var string[] |
53
|
|
|
*/ |
54
|
|
|
private array $stringTable = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Private unique Conditional HashTable. |
58
|
|
|
* |
59
|
|
|
* @var HashTable<Conditional> |
60
|
|
|
*/ |
61
|
|
|
private HashTable $stylesConditionalHashTable; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Private unique Style HashTable. |
65
|
|
|
* |
66
|
|
|
* @var HashTable<\PhpOffice\PhpSpreadsheet\Style\Style> |
67
|
|
|
*/ |
68
|
|
|
private HashTable $styleHashTable; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Private unique Fill HashTable. |
72
|
|
|
* |
73
|
|
|
* @var HashTable<Fill> |
74
|
|
|
*/ |
75
|
|
|
private HashTable $fillHashTable; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Private unique \PhpOffice\PhpSpreadsheet\Style\Font HashTable. |
79
|
|
|
* |
80
|
|
|
* @var HashTable<Font> |
81
|
|
|
*/ |
82
|
|
|
private HashTable $fontHashTable; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Private unique Borders HashTable. |
86
|
|
|
* |
87
|
|
|
* @var HashTable<Borders> |
88
|
|
|
*/ |
89
|
|
|
private HashTable $bordersHashTable; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Private unique NumberFormat HashTable. |
93
|
|
|
* |
94
|
|
|
* @var HashTable<NumberFormat> |
95
|
|
|
*/ |
96
|
|
|
private HashTable $numFmtHashTable; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Private unique \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\BaseDrawing HashTable. |
100
|
|
|
* |
101
|
|
|
* @var HashTable<BaseDrawing> |
102
|
|
|
*/ |
103
|
|
|
private HashTable $drawingHashTable; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Private handle for zip stream. |
107
|
|
|
*/ |
108
|
|
|
private ZipStream $zip; |
109
|
|
|
|
110
|
|
|
private Chart $writerPartChart; |
111
|
|
|
|
112
|
|
|
private Comments $writerPartComments; |
113
|
|
|
|
114
|
|
|
private ContentTypes $writerPartContentTypes; |
115
|
|
|
|
116
|
|
|
private DocProps $writerPartDocProps; |
117
|
|
|
|
118
|
|
|
private Drawing $writerPartDrawing; |
119
|
|
|
|
120
|
|
|
private Rels $writerPartRels; |
121
|
|
|
|
122
|
|
|
private RelsRibbon $writerPartRelsRibbon; |
123
|
|
|
|
124
|
|
|
private RelsVBA $writerPartRelsVBA; |
125
|
|
|
|
126
|
|
|
private StringTable $writerPartStringTable; |
127
|
|
|
|
128
|
|
|
private Style $writerPartStyle; |
129
|
|
|
|
130
|
|
|
private Theme $writerPartTheme; |
131
|
|
|
|
132
|
|
|
private Table $writerPartTable; |
133
|
|
|
|
134
|
|
|
private Workbook $writerPartWorkbook; |
135
|
|
|
|
136
|
|
|
private Worksheet $writerPartWorksheet; |
137
|
|
|
|
138
|
|
|
private bool $explicitStyle0 = false; |
139
|
|
|
|
140
|
|
|
private bool $useCSEArrays = false; |
141
|
|
|
|
142
|
|
|
private bool $useDynamicArray = false; |
143
|
|
|
|
144
|
|
|
public const DEFAULT_FORCE_FULL_CALC = false; |
145
|
|
|
|
146
|
|
|
// Default changed from null in PhpSpreadsheet 4.0.0. |
147
|
|
|
private ?bool $forceFullCalc = self::DEFAULT_FORCE_FULL_CALC; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Create a new Xlsx Writer. |
151
|
|
|
*/ |
152
|
454 |
|
public function __construct(Spreadsheet $spreadsheet) |
153
|
|
|
{ |
154
|
|
|
// Assign PhpSpreadsheet |
155
|
454 |
|
$this->setSpreadsheet($spreadsheet); |
156
|
|
|
|
157
|
454 |
|
$this->writerPartChart = new Chart($this); |
158
|
454 |
|
$this->writerPartComments = new Comments($this); |
159
|
454 |
|
$this->writerPartContentTypes = new ContentTypes($this); |
160
|
454 |
|
$this->writerPartDocProps = new DocProps($this); |
161
|
454 |
|
$this->writerPartDrawing = new Drawing($this); |
162
|
454 |
|
$this->writerPartRels = new Rels($this); |
163
|
454 |
|
$this->writerPartRelsRibbon = new RelsRibbon($this); |
164
|
454 |
|
$this->writerPartRelsVBA = new RelsVBA($this); |
165
|
454 |
|
$this->writerPartStringTable = new StringTable($this); |
166
|
454 |
|
$this->writerPartStyle = new Style($this); |
167
|
454 |
|
$this->writerPartTheme = new Theme($this); |
168
|
454 |
|
$this->writerPartTable = new Table($this); |
169
|
454 |
|
$this->writerPartWorkbook = new Workbook($this); |
170
|
454 |
|
$this->writerPartWorksheet = new Worksheet($this); |
171
|
|
|
|
172
|
|
|
// Set HashTable variables |
173
|
454 |
|
$this->bordersHashTable = new HashTable(); |
174
|
454 |
|
$this->drawingHashTable = new HashTable(); |
175
|
454 |
|
$this->fillHashTable = new HashTable(); |
176
|
454 |
|
$this->fontHashTable = new HashTable(); |
177
|
454 |
|
$this->numFmtHashTable = new HashTable(); |
178
|
454 |
|
$this->styleHashTable = new HashTable(); |
179
|
454 |
|
$this->stylesConditionalHashTable = new HashTable(); |
180
|
454 |
|
$this->determineUseDynamicArrays(); |
181
|
|
|
} |
182
|
|
|
|
183
|
78 |
|
public function getWriterPartChart(): Chart |
184
|
|
|
{ |
185
|
78 |
|
return $this->writerPartChart; |
186
|
|
|
} |
187
|
|
|
|
188
|
26 |
|
public function getWriterPartComments(): Comments |
189
|
|
|
{ |
190
|
26 |
|
return $this->writerPartComments; |
191
|
|
|
} |
192
|
|
|
|
193
|
392 |
|
public function getWriterPartContentTypes(): ContentTypes |
194
|
|
|
{ |
195
|
392 |
|
return $this->writerPartContentTypes; |
196
|
|
|
} |
197
|
|
|
|
198
|
392 |
|
public function getWriterPartDocProps(): DocProps |
199
|
|
|
{ |
200
|
392 |
|
return $this->writerPartDocProps; |
201
|
|
|
} |
202
|
|
|
|
203
|
392 |
|
public function getWriterPartDrawing(): Drawing |
204
|
|
|
{ |
205
|
392 |
|
return $this->writerPartDrawing; |
206
|
|
|
} |
207
|
|
|
|
208
|
392 |
|
public function getWriterPartRels(): Rels |
209
|
|
|
{ |
210
|
392 |
|
return $this->writerPartRels; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getWriterPartRelsRibbon(): RelsRibbon |
214
|
|
|
{ |
215
|
|
|
return $this->writerPartRelsRibbon; |
216
|
|
|
} |
217
|
|
|
|
218
|
2 |
|
public function getWriterPartRelsVBA(): RelsVBA |
219
|
|
|
{ |
220
|
2 |
|
return $this->writerPartRelsVBA; |
221
|
|
|
} |
222
|
|
|
|
223
|
449 |
|
public function getWriterPartStringTable(): StringTable |
224
|
|
|
{ |
225
|
449 |
|
return $this->writerPartStringTable; |
226
|
|
|
} |
227
|
|
|
|
228
|
395 |
|
public function getWriterPartStyle(): Style |
229
|
|
|
{ |
230
|
395 |
|
return $this->writerPartStyle; |
231
|
|
|
} |
232
|
|
|
|
233
|
392 |
|
public function getWriterPartTheme(): Theme |
234
|
|
|
{ |
235
|
392 |
|
return $this->writerPartTheme; |
236
|
|
|
} |
237
|
|
|
|
238
|
8 |
|
public function getWriterPartTable(): Table |
239
|
|
|
{ |
240
|
8 |
|
return $this->writerPartTable; |
241
|
|
|
} |
242
|
|
|
|
243
|
392 |
|
public function getWriterPartWorkbook(): Workbook |
244
|
|
|
{ |
245
|
392 |
|
return $this->writerPartWorkbook; |
246
|
|
|
} |
247
|
|
|
|
248
|
392 |
|
public function getWriterPartWorksheet(): Worksheet |
249
|
|
|
{ |
250
|
392 |
|
return $this->writerPartWorksheet; |
251
|
|
|
} |
252
|
|
|
|
253
|
394 |
|
public function createStyleDictionaries(): void |
254
|
|
|
{ |
255
|
394 |
|
$this->styleHashTable->addFromSource( |
256
|
394 |
|
$this->getWriterPartStyle()->allStyles( |
257
|
394 |
|
$this->spreadSheet |
258
|
394 |
|
) |
259
|
394 |
|
); |
260
|
394 |
|
$this->stylesConditionalHashTable->addFromSource( |
261
|
394 |
|
$this->getWriterPartStyle()->allConditionalStyles( |
262
|
394 |
|
$this->spreadSheet |
263
|
394 |
|
) |
264
|
394 |
|
); |
265
|
394 |
|
$this->fillHashTable->addFromSource( |
266
|
394 |
|
$this->getWriterPartStyle()->allFills( |
267
|
394 |
|
$this->spreadSheet |
268
|
394 |
|
) |
269
|
394 |
|
); |
270
|
394 |
|
$this->fontHashTable->addFromSource( |
271
|
394 |
|
$this->getWriterPartStyle()->allFonts( |
272
|
394 |
|
$this->spreadSheet |
273
|
394 |
|
) |
274
|
394 |
|
); |
275
|
394 |
|
$this->bordersHashTable->addFromSource( |
276
|
394 |
|
$this->getWriterPartStyle()->allBorders( |
277
|
394 |
|
$this->spreadSheet |
278
|
394 |
|
) |
279
|
394 |
|
); |
280
|
394 |
|
$this->numFmtHashTable->addFromSource( |
281
|
394 |
|
$this->getWriterPartStyle()->allNumberFormats( |
282
|
394 |
|
$this->spreadSheet |
283
|
394 |
|
) |
284
|
394 |
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return (RichText|string)[] $stringTable |
289
|
|
|
*/ |
290
|
393 |
|
public function createStringTable(): array |
291
|
|
|
{ |
292
|
393 |
|
$this->stringTable = []; |
293
|
393 |
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
294
|
393 |
|
$this->stringTable = $this->getWriterPartStringTable()->createStringTable($this->spreadSheet->getSheet($i), $this->stringTable); |
295
|
|
|
} |
296
|
|
|
|
297
|
393 |
|
return $this->stringTable; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Save PhpSpreadsheet to file. |
302
|
|
|
* |
303
|
|
|
* @param resource|string $filename |
304
|
|
|
*/ |
305
|
392 |
|
public function save($filename, int $flags = 0): void |
306
|
|
|
{ |
307
|
392 |
|
$this->processFlags($flags); |
308
|
392 |
|
$this->determineUseDynamicArrays(); |
309
|
|
|
|
310
|
|
|
// garbage collect |
311
|
392 |
|
$this->pathNames = []; |
312
|
392 |
|
$this->spreadSheet->garbageCollect(); |
313
|
|
|
|
314
|
392 |
|
$saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog(); |
315
|
392 |
|
Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false); |
316
|
392 |
|
$saveDateReturnType = Functions::getReturnDateType(); |
317
|
392 |
|
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); |
318
|
|
|
|
319
|
|
|
// Create string lookup table |
320
|
392 |
|
$this->createStringTable(); |
321
|
|
|
|
322
|
|
|
// Create styles dictionaries |
323
|
392 |
|
$this->createStyleDictionaries(); |
324
|
|
|
|
325
|
|
|
// Create drawing dictionary |
326
|
392 |
|
$this->drawingHashTable->addFromSource($this->getWriterPartDrawing()->allDrawings($this->spreadSheet)); |
327
|
|
|
|
328
|
|
|
/** @var string[] */ |
329
|
392 |
|
$zipContent = []; |
330
|
|
|
// Add [Content_Types].xml to ZIP file |
331
|
392 |
|
$zipContent['[Content_Types].xml'] = $this->getWriterPartContentTypes()->writeContentTypes($this->spreadSheet, $this->includeCharts); |
332
|
392 |
|
$metadataData = (new Xlsx\Metadata($this))->writeMetadata(); |
333
|
392 |
|
if ($metadataData !== '') { |
334
|
11 |
|
$zipContent['xl/metadata.xml'] = $metadataData; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
//if hasMacros, add the vbaProject.bin file, Certificate file(if exists) |
338
|
392 |
|
if ($this->spreadSheet->hasMacros()) { |
339
|
2 |
|
$macrosCode = $this->spreadSheet->getMacrosCode(); |
340
|
2 |
|
if ($macrosCode !== null) { |
341
|
|
|
// we have the code ? |
342
|
2 |
|
$zipContent['xl/vbaProject.bin'] = $macrosCode; //allways in 'xl', allways named vbaProject.bin |
343
|
2 |
|
if ($this->spreadSheet->hasMacrosCertificate()) { |
344
|
|
|
//signed macros ? |
345
|
|
|
// Yes : add the certificate file and the related rels file |
346
|
2 |
|
$zipContent['xl/vbaProjectSignature.bin'] = $this->spreadSheet->getMacrosCertificate(); |
347
|
2 |
|
$zipContent['xl/_rels/vbaProject.bin.rels'] = $this->getWriterPartRelsVBA()->writeVBARelationships(); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels) |
352
|
392 |
|
if ($this->spreadSheet->hasRibbon()) { |
353
|
2 |
|
$tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target'); |
354
|
2 |
|
$tmpRibbonTarget = is_string($tmpRibbonTarget) ? $tmpRibbonTarget : ''; |
355
|
2 |
|
$zipContent[$tmpRibbonTarget] = $this->spreadSheet->getRibbonXMLData('data'); |
356
|
2 |
|
if ($this->spreadSheet->hasRibbonBinObjects()) { |
357
|
|
|
$tmpRootPath = dirname($tmpRibbonTarget) . '/'; |
358
|
|
|
$ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write |
359
|
|
|
if (is_array($ribbonBinObjects)) { |
360
|
|
|
foreach ($ribbonBinObjects as $aPath => $aContent) { |
361
|
|
|
$zipContent[$tmpRootPath . $aPath] = $aContent; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
//the rels for files |
365
|
|
|
$zipContent[$tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels'] = $this->getWriterPartRelsRibbon()->writeRibbonRelationships($this->spreadSheet); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
// Add relationships to ZIP file |
370
|
392 |
|
$zipContent['_rels/.rels'] = $this->getWriterPartRels()->writeRelationships($this->spreadSheet); |
371
|
392 |
|
$zipContent['xl/_rels/workbook.xml.rels'] = $this->getWriterPartRels()->writeWorkbookRelationships($this->spreadSheet); |
372
|
|
|
|
373
|
|
|
// Add document properties to ZIP file |
374
|
392 |
|
$zipContent['docProps/app.xml'] = $this->getWriterPartDocProps()->writeDocPropsApp($this->spreadSheet); |
375
|
392 |
|
$zipContent['docProps/core.xml'] = $this->getWriterPartDocProps()->writeDocPropsCore($this->spreadSheet); |
376
|
392 |
|
$customPropertiesPart = $this->getWriterPartDocProps()->writeDocPropsCustom($this->spreadSheet); |
377
|
392 |
|
if ($customPropertiesPart !== null) { |
378
|
30 |
|
$zipContent['docProps/custom.xml'] = $customPropertiesPart; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
// Add theme to ZIP file |
382
|
392 |
|
$zipContent['xl/theme/theme1.xml'] = $this->getWriterPartTheme()->writeTheme($this->spreadSheet); |
383
|
|
|
|
384
|
|
|
// Add string table to ZIP file |
385
|
392 |
|
$zipContent['xl/sharedStrings.xml'] = $this->getWriterPartStringTable()->writeStringTable($this->stringTable); |
386
|
|
|
|
387
|
|
|
// Add styles to ZIP file |
388
|
392 |
|
$zipContent['xl/styles.xml'] = $this->getWriterPartStyle()->writeStyles($this->spreadSheet); |
389
|
|
|
|
390
|
|
|
// Add workbook to ZIP file |
391
|
392 |
|
$zipContent['xl/workbook.xml'] = $this->getWriterPartWorkbook()->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas, $this->forceFullCalc); |
392
|
|
|
|
393
|
392 |
|
$chartCount = 0; |
394
|
|
|
// Add worksheets |
395
|
392 |
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
396
|
392 |
|
$zipContent['xl/worksheets/sheet' . ($i + 1) . '.xml'] = $this->getWriterPartWorksheet()->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts); |
397
|
391 |
|
if ($this->includeCharts) { |
398
|
79 |
|
$charts = $this->spreadSheet->getSheet($i)->getChartCollection(); |
399
|
79 |
|
if (count($charts) > 0) { |
400
|
78 |
|
foreach ($charts as $chart) { |
401
|
78 |
|
$zipContent['xl/charts/chart' . ($chartCount + 1) . '.xml'] = $this->getWriterPartChart()->writeChart($chart, $this->preCalculateFormulas); |
402
|
78 |
|
++$chartCount; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
391 |
|
$chartRef1 = 0; |
409
|
391 |
|
$tableRef1 = 1; |
410
|
|
|
// Add worksheet relationships (drawings, ...) |
411
|
391 |
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
412
|
|
|
// Add relationships |
413
|
|
|
/** @var string[] $zipContent */ |
414
|
391 |
|
$zipContent['xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels'] = $this->getWriterPartRels()->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts, $tableRef1, $zipContent); |
415
|
|
|
|
416
|
|
|
// Add unparsedLoadedData |
417
|
391 |
|
$sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName(); |
418
|
|
|
/** @var mixed[][][] */ |
419
|
391 |
|
$unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData(); |
420
|
|
|
/** @var mixed[][] */ |
421
|
391 |
|
$unparsedSheet = $unparsedLoadedData['sheets'][$sheetCodeName] ?? []; |
422
|
391 |
|
foreach (($unparsedSheet['ctrlProps'] ?? []) as $ctrlProp) { |
423
|
|
|
/** @var string[] $ctrlProp */ |
424
|
4 |
|
$zipContent[$ctrlProp['filePath']] = $ctrlProp['content']; |
425
|
|
|
} |
426
|
391 |
|
foreach (($unparsedSheet['printerSettings'] ?? []) as $ctrlProp) { |
427
|
|
|
/** @var string[] $ctrlProp */ |
428
|
35 |
|
$zipContent[$ctrlProp['filePath']] = $ctrlProp['content']; |
429
|
|
|
} |
430
|
|
|
|
431
|
391 |
|
$drawings = $this->spreadSheet->getSheet($i)->getDrawingCollection(); |
432
|
391 |
|
$drawingCount = count($drawings); |
433
|
391 |
|
if ($this->includeCharts) { |
434
|
79 |
|
$chartCount = $this->spreadSheet->getSheet($i)->getChartCount(); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
// Add drawing and image relationship parts |
438
|
391 |
|
if (($drawingCount > 0) || ($chartCount > 0)) { |
439
|
|
|
// Drawing relationships |
440
|
120 |
|
$zipContent['xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels'] = $this->getWriterPartRels()->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts); |
441
|
|
|
|
442
|
|
|
// Drawings |
443
|
120 |
|
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $this->getWriterPartDrawing()->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts); |
444
|
287 |
|
} elseif (isset($unparsedSheet['drawingAlternateContents'])) { |
445
|
|
|
// Drawings |
446
|
4 |
|
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $this->getWriterPartDrawing()->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
// Add unparsed drawings |
450
|
391 |
|
if (isset($unparsedSheet['Drawings']) && !isset($zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'])) { |
451
|
5 |
|
foreach ($unparsedSheet['Drawings'] as $relId => $drawingXml) { |
452
|
5 |
|
$drawingFile = array_search($relId, $unparsedSheet['drawingOriginalIds']); |
453
|
5 |
|
if ($drawingFile !== false) { |
454
|
|
|
//$drawingFile = ltrim($drawingFile, '.'); |
455
|
|
|
//$zipContent['xl' . $drawingFile] = $drawingXml; |
456
|
5 |
|
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $drawingXml; |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
} |
460
|
391 |
|
if (isset($unparsedSheet['drawingOriginalIds']) && !isset($zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'])) { |
461
|
1 |
|
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = '<xml></xml>'; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// Add comment relationship parts |
465
|
|
|
/** @var mixed[][] */ |
466
|
391 |
|
$legacyTemp = $unparsedLoadedData['sheets'] ?? []; |
467
|
391 |
|
$legacyTemp = $legacyTemp[$this->spreadSheet->getSheet($i)->getCodeName()] ?? []; |
468
|
391 |
|
$legacy = $legacyTemp['legacyDrawing'] ?? null; |
469
|
391 |
|
if (count($this->spreadSheet->getSheet($i)->getComments()) > 0 || $legacy !== null) { |
470
|
|
|
// VML Comments relationships |
471
|
28 |
|
$zipContent['xl/drawings/_rels/vmlDrawing' . ($i + 1) . '.vml.rels'] = $this->getWriterPartRels()->writeVMLDrawingRelationships($this->spreadSheet->getSheet($i)); |
472
|
|
|
|
473
|
|
|
// VML Comments |
474
|
28 |
|
$zipContent['xl/drawings/vmlDrawing' . ($i + 1) . '.vml'] = $legacy ?? $this->getWriterPartComments()->writeVMLComments($this->spreadSheet->getSheet($i)); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
// Comments |
478
|
391 |
|
if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) { |
479
|
26 |
|
$zipContent['xl/comments' . ($i + 1) . '.xml'] = $this->getWriterPartComments()->writeComments($this->spreadSheet->getSheet($i)); |
480
|
|
|
|
481
|
|
|
// Media |
482
|
26 |
|
foreach ($this->spreadSheet->getSheet($i)->getComments() as $comment) { |
483
|
26 |
|
if ($comment->hasBackgroundImage()) { |
484
|
3 |
|
$image = $comment->getBackgroundImage(); |
485
|
3 |
|
$zipContent['xl/media/' . $image->getMediaFilename()] = $this->processDrawing($image); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
// Add unparsed relationship parts |
491
|
391 |
|
if (isset($unparsedSheet['vmlDrawings'])) { |
492
|
5 |
|
foreach ($unparsedSheet['vmlDrawings'] as $vmlDrawing) { |
493
|
|
|
/** @var string[] $vmlDrawing */ |
494
|
5 |
|
if (!isset($zipContent[$vmlDrawing['filePath']])) { |
495
|
3 |
|
$zipContent[$vmlDrawing['filePath']] = $vmlDrawing['content']; |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
// Add header/footer relationship parts |
501
|
391 |
|
if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { |
502
|
|
|
// VML Drawings |
503
|
3 |
|
$zipContent['xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml'] = $this->getWriterPartDrawing()->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i)); |
504
|
|
|
|
505
|
|
|
// VML Drawing relationships |
506
|
3 |
|
$zipContent['xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels'] = $this->getWriterPartRels()->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i)); |
507
|
|
|
|
508
|
|
|
// Media |
509
|
3 |
|
foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { |
510
|
3 |
|
if ($image->getPath() !== '') { |
511
|
3 |
|
$zipContent['xl/media/' . $image->getIndexedFilename()] = file_get_contents($image->getPath()); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// Add Table parts |
517
|
391 |
|
$tables = $this->spreadSheet->getSheet($i)->getTableCollection(); |
518
|
391 |
|
foreach ($tables as $table) { |
519
|
8 |
|
$zipContent['xl/tables/table' . $tableRef1 . '.xml'] = $this->getWriterPartTable()->writeTable($table, $tableRef1++); |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
// Add media |
524
|
391 |
|
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { |
525
|
53 |
|
if ($this->getDrawingHashTable()->getByIndex($i) instanceof WorksheetDrawing) { |
526
|
42 |
|
$imageContents = null; |
527
|
42 |
|
$imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); |
528
|
42 |
|
if ($imagePath === '') { |
529
|
|
|
continue; |
530
|
|
|
} |
531
|
42 |
|
if (str_contains($imagePath, 'zip://')) { |
532
|
24 |
|
$imagePath = substr($imagePath, 6); |
533
|
24 |
|
$imagePathSplitted = explode('#', $imagePath); |
534
|
|
|
|
535
|
24 |
|
$imageZip = new ZipArchive(); |
536
|
24 |
|
$imageZip->open($imagePathSplitted[0]); |
537
|
24 |
|
$imageContents = $imageZip->getFromName($imagePathSplitted[1]); |
538
|
24 |
|
$imageZip->close(); |
539
|
24 |
|
unset($imageZip); |
540
|
|
|
} else { |
541
|
20 |
|
$imageContents = file_get_contents($imagePath); |
542
|
|
|
} |
543
|
|
|
|
544
|
42 |
|
$zipContent['xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()] = $imageContents; |
545
|
11 |
|
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) { |
546
|
11 |
|
ob_start(); |
547
|
11 |
|
$callable = $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(); |
548
|
11 |
|
call_user_func( |
549
|
11 |
|
$callable, |
550
|
11 |
|
$this->getDrawingHashTable()->getByIndex($i)->getImageResource() |
551
|
11 |
|
); |
552
|
11 |
|
$imageContents = ob_get_contents(); |
553
|
11 |
|
ob_end_clean(); |
554
|
|
|
|
555
|
11 |
|
$zipContent['xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()] = $imageContents; |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
391 |
|
Functions::setReturnDateType($saveDateReturnType); |
560
|
391 |
|
Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
561
|
|
|
|
562
|
391 |
|
$this->openFileHandle($filename); |
563
|
|
|
|
564
|
391 |
|
$this->zip = ZipStream0::newZipStream($this->fileHandle); |
565
|
|
|
|
566
|
|
|
/** @var string[] $zipContent */ |
567
|
391 |
|
$this->addZipFiles($zipContent); |
568
|
|
|
|
569
|
|
|
// Close file |
570
|
|
|
try { |
571
|
391 |
|
$this->zip->finish(); |
572
|
|
|
} catch (OverflowException) { |
573
|
|
|
throw new WriterException('Could not close resource.'); |
574
|
|
|
} |
575
|
|
|
|
576
|
391 |
|
$this->maybeCloseFileHandle(); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Get Spreadsheet object. |
581
|
|
|
*/ |
582
|
449 |
|
public function getSpreadsheet(): Spreadsheet |
583
|
|
|
{ |
584
|
449 |
|
return $this->spreadSheet; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Set Spreadsheet object. |
589
|
|
|
* |
590
|
|
|
* @param Spreadsheet $spreadsheet PhpSpreadsheet object |
591
|
|
|
* |
592
|
|
|
* @return $this |
593
|
|
|
*/ |
594
|
454 |
|
public function setSpreadsheet(Spreadsheet $spreadsheet): static |
595
|
|
|
{ |
596
|
454 |
|
$this->spreadSheet = $spreadsheet; |
597
|
|
|
|
598
|
454 |
|
return $this; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Get string table. |
603
|
|
|
* |
604
|
|
|
* @return string[] |
605
|
|
|
*/ |
606
|
|
|
public function getStringTable(): array |
607
|
|
|
{ |
608
|
|
|
return $this->stringTable; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Get Style HashTable. |
613
|
|
|
* |
614
|
|
|
* @return HashTable<\PhpOffice\PhpSpreadsheet\Style\Style> |
615
|
|
|
*/ |
616
|
|
|
public function getStyleHashTable(): HashTable |
617
|
|
|
{ |
618
|
|
|
return $this->styleHashTable; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Get Conditional HashTable. |
623
|
|
|
* |
624
|
|
|
* @return HashTable<Conditional> |
625
|
|
|
*/ |
626
|
429 |
|
public function getStylesConditionalHashTable(): HashTable |
627
|
|
|
{ |
628
|
429 |
|
return $this->stylesConditionalHashTable; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Get Fill HashTable. |
633
|
|
|
* |
634
|
|
|
* @return HashTable<Fill> |
635
|
|
|
*/ |
636
|
395 |
|
public function getFillHashTable(): HashTable |
637
|
|
|
{ |
638
|
395 |
|
return $this->fillHashTable; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Get \PhpOffice\PhpSpreadsheet\Style\Font HashTable. |
643
|
|
|
* |
644
|
|
|
* @return HashTable<Font> |
645
|
|
|
*/ |
646
|
395 |
|
public function getFontHashTable(): HashTable |
647
|
|
|
{ |
648
|
395 |
|
return $this->fontHashTable; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Get Borders HashTable. |
653
|
|
|
* |
654
|
|
|
* @return HashTable<Borders> |
655
|
|
|
*/ |
656
|
395 |
|
public function getBordersHashTable(): HashTable |
657
|
|
|
{ |
658
|
395 |
|
return $this->bordersHashTable; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* Get NumberFormat HashTable. |
663
|
|
|
* |
664
|
|
|
* @return HashTable<NumberFormat> |
665
|
|
|
*/ |
666
|
395 |
|
public function getNumFmtHashTable(): HashTable |
667
|
|
|
{ |
668
|
395 |
|
return $this->numFmtHashTable; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Get \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\BaseDrawing HashTable. |
673
|
|
|
* |
674
|
|
|
* @return HashTable<BaseDrawing> |
675
|
|
|
*/ |
676
|
392 |
|
public function getDrawingHashTable(): HashTable |
677
|
|
|
{ |
678
|
392 |
|
return $this->drawingHashTable; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Get Office2003 compatibility. |
683
|
|
|
*/ |
684
|
395 |
|
public function getOffice2003Compatibility(): bool |
685
|
|
|
{ |
686
|
395 |
|
return $this->office2003compatibility; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Set Office2003 compatibility. |
691
|
|
|
* |
692
|
|
|
* @param bool $office2003compatibility Office2003 compatibility? |
693
|
|
|
* |
694
|
|
|
* @return $this |
695
|
|
|
*/ |
696
|
|
|
public function setOffice2003Compatibility(bool $office2003compatibility): static |
697
|
|
|
{ |
698
|
|
|
$this->office2003compatibility = $office2003compatibility; |
699
|
|
|
|
700
|
|
|
return $this; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** @var string[] */ |
704
|
|
|
private array $pathNames = []; |
705
|
|
|
|
706
|
391 |
|
private function addZipFile(string $path, string $content): void |
707
|
|
|
{ |
708
|
391 |
|
if (!in_array($path, $this->pathNames)) { |
709
|
391 |
|
$this->pathNames[] = $path; |
710
|
391 |
|
$this->zip->addFile($path, $content); |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** @param string[] $zipContent */ |
715
|
391 |
|
private function addZipFiles(array $zipContent): void |
716
|
|
|
{ |
717
|
391 |
|
foreach ($zipContent as $path => $content) { |
718
|
391 |
|
$this->addZipFile($path, $content); |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
|
722
|
3 |
|
private function processDrawing(WorksheetDrawing $drawing): string|null|false |
723
|
|
|
{ |
724
|
3 |
|
$data = null; |
725
|
3 |
|
$filename = $drawing->getPath(); |
726
|
3 |
|
if ($filename === '') { |
727
|
|
|
return null; |
728
|
|
|
} |
729
|
3 |
|
$imageData = getimagesize($filename); |
730
|
|
|
|
731
|
3 |
|
if (!empty($imageData)) { |
732
|
3 |
|
switch ($imageData[2]) { |
733
|
3 |
|
case 1: // GIF, not supported by BIFF8, we convert to PNG |
734
|
2 |
|
$image = imagecreatefromgif($filename); |
735
|
2 |
|
if ($image !== false) { |
736
|
2 |
|
ob_start(); |
737
|
2 |
|
imagepng($image); |
738
|
2 |
|
$data = ob_get_contents(); |
739
|
2 |
|
ob_end_clean(); |
740
|
|
|
} |
741
|
|
|
|
742
|
2 |
|
break; |
743
|
|
|
|
744
|
3 |
|
case 2: // JPEG |
745
|
2 |
|
$data = file_get_contents($filename); |
746
|
|
|
|
747
|
2 |
|
break; |
748
|
|
|
|
749
|
2 |
|
case 3: // PNG |
750
|
1 |
|
$data = file_get_contents($filename); |
751
|
|
|
|
752
|
1 |
|
break; |
753
|
|
|
|
754
|
2 |
|
case 6: // Windows DIB (BMP), we convert to PNG |
755
|
2 |
|
$image = imagecreatefrombmp($filename); |
756
|
2 |
|
if ($image !== false) { |
757
|
2 |
|
ob_start(); |
758
|
2 |
|
imagepng($image); |
759
|
2 |
|
$data = ob_get_contents(); |
760
|
2 |
|
ob_end_clean(); |
761
|
|
|
} |
762
|
|
|
|
763
|
2 |
|
break; |
764
|
|
|
} |
765
|
|
|
} |
766
|
|
|
|
767
|
3 |
|
return $data; |
768
|
|
|
} |
769
|
|
|
|
770
|
436 |
|
public function getExplicitStyle0(): bool |
771
|
|
|
{ |
772
|
436 |
|
return $this->explicitStyle0; |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* This may be useful if non-default Alignment is part of default style |
777
|
|
|
* and you think you might want to open the spreadsheet |
778
|
|
|
* with LibreOffice or Gnumeric. |
779
|
|
|
*/ |
780
|
1 |
|
public function setExplicitStyle0(bool $explicitStyle0): self |
781
|
|
|
{ |
782
|
1 |
|
$this->explicitStyle0 = $explicitStyle0; |
783
|
|
|
|
784
|
1 |
|
return $this; |
785
|
|
|
} |
786
|
|
|
|
787
|
3 |
|
public function setUseCSEArrays(?bool $useCSEArrays): void |
788
|
|
|
{ |
789
|
3 |
|
if ($useCSEArrays !== null) { |
790
|
3 |
|
$this->useCSEArrays = $useCSEArrays; |
791
|
|
|
} |
792
|
3 |
|
$this->determineUseDynamicArrays(); |
793
|
|
|
} |
794
|
|
|
|
795
|
437 |
|
public function useDynamicArrays(): bool |
796
|
|
|
{ |
797
|
437 |
|
return $this->useDynamicArray; |
798
|
|
|
} |
799
|
|
|
|
800
|
454 |
|
private function determineUseDynamicArrays(): void |
801
|
|
|
{ |
802
|
454 |
|
$this->useDynamicArray = $this->preCalculateFormulas && Calculation::getInstance($this->spreadSheet)->getInstanceArrayReturnType() === Calculation::RETURN_ARRAY_AS_ARRAY && !$this->useCSEArrays; |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* If this is set when a spreadsheet is opened, |
807
|
|
|
* values may not be automatically re-calculated, |
808
|
|
|
* and a button will be available to force re-calculation. |
809
|
|
|
* This may apply to all spreadsheets open at that time. |
810
|
|
|
* If null, this will be set to the opposite of $preCalculateFormulas. |
811
|
|
|
* It is likely that false is the desired setting, although |
812
|
|
|
* cases have been reported where true is required (issue #456). |
813
|
|
|
* Nevertheless, default is set to false in PhpSpreadsheet 4.0.0. |
814
|
|
|
*/ |
815
|
4 |
|
public function setForceFullCalc(?bool $forceFullCalc): self |
816
|
|
|
{ |
817
|
4 |
|
$this->forceFullCalc = $forceFullCalc; |
818
|
|
|
|
819
|
4 |
|
return $this; |
820
|
|
|
} |
821
|
|
|
} |
822
|
|
|
|