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