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