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