1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer\Xls; |
4
|
|
|
|
5
|
|
|
use Composer\Pcre\Preg; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\DefinedName; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
14
|
|
|
|
15
|
|
|
// Original file header of PEAR::Spreadsheet_Excel_Writer_Workbook (used as the base for this class): |
16
|
|
|
// ----------------------------------------------------------------------------------------- |
17
|
|
|
// /* |
18
|
|
|
// * Module written/ported by Xavier Noguer <[email protected]> |
19
|
|
|
// * |
20
|
|
|
// * The majority of this is _NOT_ my code. I simply ported it from the |
21
|
|
|
// * PERL Spreadsheet::WriteExcel module. |
22
|
|
|
// * |
23
|
|
|
// * The author of the Spreadsheet::WriteExcel module is John McNamara |
24
|
|
|
// * <[email protected]> |
25
|
|
|
// * |
26
|
|
|
// * I _DO_ maintain this code, and John McNamara has nothing to do with the |
27
|
|
|
// * porting of this code to PHP. Any questions directly related to this |
28
|
|
|
// * class library should be directed to me. |
29
|
|
|
// * |
30
|
|
|
// * License Information: |
31
|
|
|
// * |
32
|
|
|
// * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets |
33
|
|
|
// * Copyright (c) 2002-2003 Xavier Noguer [email protected] |
34
|
|
|
// * |
35
|
|
|
// * This library is free software; you can redistribute it and/or |
36
|
|
|
// * modify it under the terms of the GNU Lesser General Public |
37
|
|
|
// * License as published by the Free Software Foundation; either |
38
|
|
|
// * version 2.1 of the License, or (at your option) any later version. |
39
|
|
|
// * |
40
|
|
|
// * This library is distributed in the hope that it will be useful, |
41
|
|
|
// * but WITHOUT ANY WARRANTY; without even the implied warranty of |
42
|
|
|
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
43
|
|
|
// * Lesser General Public License for more details. |
44
|
|
|
// * |
45
|
|
|
// * You should have received a copy of the GNU Lesser General Public |
46
|
|
|
// * License along with this library; if not, write to the Free Software |
47
|
|
|
// * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
48
|
|
|
// */ |
49
|
|
|
class Workbook extends BIFFwriter |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Formula parser. |
53
|
|
|
*/ |
54
|
|
|
private Parser $parser; |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* The BIFF file size for the workbook. Not currently used. |
58
|
|
|
* |
59
|
|
|
* @see calcSheetOffsets() |
60
|
|
|
*/ |
61
|
|
|
//private int $biffSize; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* XF Writers. |
65
|
|
|
* |
66
|
|
|
* @var Xf[] |
67
|
|
|
*/ |
68
|
|
|
private array $xfWriters = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Array containing the colour palette. |
72
|
|
|
*/ |
73
|
|
|
private array $palette; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The codepage indicates the text encoding used for strings. |
77
|
|
|
*/ |
78
|
|
|
private int $codepage; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The country code used for localization. |
82
|
|
|
*/ |
83
|
|
|
private int $countryCode; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Workbook. |
87
|
|
|
*/ |
88
|
|
|
private Spreadsheet $spreadsheet; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Fonts writers. |
92
|
|
|
* |
93
|
|
|
* @var Font[] |
94
|
|
|
*/ |
95
|
|
|
private array $fontWriters = []; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Added fonts. Maps from font's hash => index in workbook. |
99
|
|
|
*/ |
100
|
|
|
private array $addedFonts = []; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Shared number formats. |
104
|
|
|
*/ |
105
|
|
|
private array $numberFormats = []; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Added number formats. Maps from numberFormat's hash => index in workbook. |
109
|
|
|
*/ |
110
|
|
|
private array $addedNumberFormats = []; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sizes of the binary worksheet streams. |
114
|
|
|
*/ |
115
|
|
|
private array $worksheetSizes = []; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Offsets of the binary worksheet streams relative to the start of the global workbook stream. |
119
|
|
|
*/ |
120
|
|
|
private array $worksheetOffsets = []; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Total number of shared strings in workbook. |
124
|
|
|
*/ |
125
|
|
|
private int $stringTotal; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Number of unique shared strings in workbook. |
129
|
|
|
*/ |
130
|
|
|
private int $stringUnique; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Array of unique shared strings in workbook. |
134
|
|
|
*/ |
135
|
|
|
private array $stringTable; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Color cache. |
139
|
|
|
*/ |
140
|
|
|
private array $colors; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Escher object corresponding to MSODRAWINGGROUP. |
144
|
|
|
*/ |
145
|
|
|
private ?\PhpOffice\PhpSpreadsheet\Shared\Escher $escher = null; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Class constructor. |
149
|
|
|
* |
150
|
|
|
* @param Spreadsheet $spreadsheet The Workbook |
151
|
|
|
* @param int $str_total Total number of strings |
152
|
|
|
* @param int $str_unique Total number of unique strings |
153
|
|
|
* @param array $str_table String Table |
154
|
|
|
* @param array $colors Colour Table |
155
|
|
|
* @param Parser $parser The formula parser created for the Workbook |
156
|
|
|
*/ |
157
|
115 |
|
public function __construct(Spreadsheet $spreadsheet, int &$str_total, int &$str_unique, array &$str_table, array &$colors, Parser $parser) |
158
|
|
|
{ |
159
|
|
|
// It needs to call its parent's constructor explicitly |
160
|
115 |
|
parent::__construct(); |
161
|
|
|
|
162
|
115 |
|
$this->parser = $parser; |
163
|
|
|
//$this->biffSize = 0; |
164
|
115 |
|
$this->palette = []; |
165
|
115 |
|
$this->countryCode = -1; |
166
|
|
|
|
167
|
115 |
|
$this->stringTotal = &$str_total; |
168
|
115 |
|
$this->stringUnique = &$str_unique; |
169
|
115 |
|
$this->stringTable = &$str_table; |
170
|
115 |
|
$this->colors = &$colors; |
171
|
115 |
|
$this->setPaletteXl97(); |
172
|
|
|
|
173
|
115 |
|
$this->spreadsheet = $spreadsheet; |
174
|
|
|
|
175
|
115 |
|
$this->codepage = 0x04B0; |
176
|
|
|
|
177
|
|
|
// Add empty sheets and Build color cache |
178
|
115 |
|
$countSheets = $spreadsheet->getSheetCount(); |
179
|
115 |
|
for ($i = 0; $i < $countSheets; ++$i) { |
180
|
115 |
|
$phpSheet = $spreadsheet->getSheet($i); |
181
|
|
|
|
182
|
115 |
|
$this->parser->setExtSheet($phpSheet->getTitle(), $i); // Register worksheet name with parser |
183
|
|
|
|
184
|
115 |
|
$supbook_index = 0x00; |
185
|
115 |
|
$ref = pack('vvv', $supbook_index, $i, $i); |
186
|
115 |
|
$this->parser->references[] = $ref; // Register reference with parser |
187
|
|
|
|
188
|
|
|
// Sheet tab colors? |
189
|
115 |
|
if ($phpSheet->isTabColorSet()) { |
190
|
6 |
|
$this->addColor($phpSheet->getTabColor()->getRGB()); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Add a new XF writer. |
197
|
|
|
* |
198
|
|
|
* @param bool $isStyleXf Is it a style XF? |
199
|
|
|
* |
200
|
|
|
* @return int Index to XF record |
201
|
|
|
*/ |
202
|
114 |
|
public function addXfWriter(Style $style, bool $isStyleXf = false): int |
203
|
|
|
{ |
204
|
114 |
|
$xfWriter = new Xf($style); |
205
|
114 |
|
$xfWriter->setIsStyleXf($isStyleXf); |
206
|
|
|
|
207
|
|
|
// Add the font if not already added |
208
|
114 |
|
$fontIndex = $this->addFont($style->getFont()); |
209
|
|
|
|
210
|
|
|
// Assign the font index to the xf record |
211
|
114 |
|
$xfWriter->setFontIndex($fontIndex); |
212
|
|
|
|
213
|
|
|
// Background colors, best to treat these after the font so black will come after white in custom palette |
214
|
114 |
|
if ($style->getFill()->getStartColor()->getRGB()) { |
215
|
114 |
|
$xfWriter->setFgColor( |
216
|
114 |
|
$this->addColor( |
217
|
114 |
|
$style->getFill()->getStartColor()->getRGB() |
218
|
114 |
|
) |
219
|
114 |
|
); |
220
|
|
|
} |
221
|
114 |
|
if ($style->getFill()->getEndColor()->getRGB()) { |
222
|
114 |
|
$xfWriter->setBgColor( |
223
|
114 |
|
$this->addColor( |
224
|
114 |
|
$style->getFill()->getEndColor()->getRGB() |
225
|
114 |
|
) |
226
|
114 |
|
); |
227
|
|
|
} |
228
|
114 |
|
$xfWriter->setBottomColor($this->addColor($style->getBorders()->getBottom()->getColor()->getRGB())); |
229
|
114 |
|
$xfWriter->setTopColor($this->addColor($style->getBorders()->getTop()->getColor()->getRGB())); |
230
|
114 |
|
$xfWriter->setRightColor($this->addColor($style->getBorders()->getRight()->getColor()->getRGB())); |
231
|
114 |
|
$xfWriter->setLeftColor($this->addColor($style->getBorders()->getLeft()->getColor()->getRGB())); |
232
|
114 |
|
$xfWriter->setDiagColor($this->addColor($style->getBorders()->getDiagonal()->getColor()->getRGB())); |
233
|
|
|
|
234
|
|
|
// Add the number format if it is not a built-in one and not already added |
235
|
114 |
|
if ($style->getNumberFormat()->getBuiltInFormatCode() === false) { |
236
|
22 |
|
$numberFormatHashCode = $style->getNumberFormat()->getHashCode(); |
237
|
|
|
|
238
|
22 |
|
if (isset($this->addedNumberFormats[$numberFormatHashCode])) { |
239
|
10 |
|
$numberFormatIndex = $this->addedNumberFormats[$numberFormatHashCode]; |
240
|
|
|
} else { |
241
|
22 |
|
$numberFormatIndex = 164 + count($this->numberFormats); |
242
|
22 |
|
$this->numberFormats[$numberFormatIndex] = $style->getNumberFormat(); |
243
|
22 |
|
$this->addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex; |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
114 |
|
$numberFormatIndex = (int) $style->getNumberFormat()->getBuiltInFormatCode(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Assign the number format index to xf record |
250
|
114 |
|
$xfWriter->setNumberFormatIndex($numberFormatIndex); |
251
|
|
|
|
252
|
114 |
|
$this->xfWriters[] = $xfWriter; |
253
|
|
|
|
254
|
114 |
|
return count($this->xfWriters) - 1; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Add a font to added fonts. |
259
|
|
|
* |
260
|
|
|
* @return int Index to FONT record |
261
|
|
|
*/ |
262
|
114 |
|
public function addFont(\PhpOffice\PhpSpreadsheet\Style\Font $font): int |
263
|
|
|
{ |
264
|
114 |
|
$fontHashCode = $font->getHashCode(); |
265
|
114 |
|
if (isset($this->addedFonts[$fontHashCode])) { |
266
|
114 |
|
$fontIndex = $this->addedFonts[$fontHashCode]; |
267
|
|
|
} else { |
268
|
114 |
|
$countFonts = count($this->fontWriters); |
269
|
114 |
|
$fontIndex = ($countFonts < 4) ? $countFonts : $countFonts + 1; |
270
|
|
|
|
271
|
114 |
|
$fontWriter = new Font($font); |
272
|
114 |
|
$fontWriter->setColorIndex($this->addColor($font->getColor()->getRGB())); |
273
|
114 |
|
$this->fontWriters[] = $fontWriter; |
274
|
|
|
|
275
|
114 |
|
$this->addedFonts[$fontHashCode] = $fontIndex; |
276
|
|
|
} |
277
|
|
|
|
278
|
114 |
|
return $fontIndex; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Alter color palette adding a custom color. |
283
|
|
|
* |
284
|
|
|
* @param string $rgb E.g. 'FF00AA' |
285
|
|
|
* |
286
|
|
|
* @return int Color index |
287
|
|
|
*/ |
288
|
115 |
|
public function addColor(string $rgb, int $default = 0): int |
289
|
|
|
{ |
290
|
115 |
|
if (!isset($this->colors[$rgb])) { |
291
|
115 |
|
$color |
292
|
115 |
|
= [ |
293
|
115 |
|
hexdec(substr($rgb, 0, 2)), |
294
|
115 |
|
hexdec(substr($rgb, 2, 2)), |
295
|
115 |
|
hexdec(substr($rgb, 4)), |
296
|
115 |
|
0, |
297
|
115 |
|
]; |
298
|
115 |
|
$colorIndex = array_search($color, $this->palette); |
299
|
115 |
|
if ($colorIndex) { |
300
|
115 |
|
$this->colors[$rgb] = $colorIndex; |
301
|
|
|
} else { |
302
|
18 |
|
if (count($this->colors) === 0) { |
303
|
7 |
|
$lastColor = 7; |
304
|
|
|
} else { |
305
|
18 |
|
$lastColor = end($this->colors); |
306
|
|
|
} |
307
|
18 |
|
if ($lastColor < 57) { |
308
|
|
|
// then we add a custom color altering the palette |
309
|
18 |
|
$colorIndex = $lastColor + 1; |
310
|
18 |
|
$this->palette[$colorIndex] = $color; |
311
|
18 |
|
$this->colors[$rgb] = $colorIndex; |
312
|
|
|
} else { |
313
|
|
|
// no room for more custom colors, just map to black |
314
|
1 |
|
$colorIndex = $default; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} else { |
318
|
|
|
// fetch already added custom color |
319
|
115 |
|
$colorIndex = $this->colors[$rgb]; |
320
|
|
|
} |
321
|
|
|
|
322
|
115 |
|
return $colorIndex; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Sets the colour palette to the Excel 97+ default. |
327
|
|
|
*/ |
328
|
115 |
|
private function setPaletteXl97(): void |
329
|
|
|
{ |
330
|
115 |
|
$this->palette = [ |
331
|
115 |
|
0x08 => [0x00, 0x00, 0x00, 0x00], |
332
|
115 |
|
0x09 => [0xFF, 0xFF, 0xFF, 0x00], |
333
|
115 |
|
0x0A => [0xFF, 0x00, 0x00, 0x00], |
334
|
115 |
|
0x0B => [0x00, 0xFF, 0x00, 0x00], |
335
|
115 |
|
0x0C => [0x00, 0x00, 0xFF, 0x00], |
336
|
115 |
|
0x0D => [0xFF, 0xFF, 0x00, 0x00], |
337
|
115 |
|
0x0E => [0xFF, 0x00, 0xFF, 0x00], |
338
|
115 |
|
0x0F => [0x00, 0xFF, 0xFF, 0x00], |
339
|
115 |
|
0x10 => [0x80, 0x00, 0x00, 0x00], |
340
|
115 |
|
0x11 => [0x00, 0x80, 0x00, 0x00], |
341
|
115 |
|
0x12 => [0x00, 0x00, 0x80, 0x00], |
342
|
115 |
|
0x13 => [0x80, 0x80, 0x00, 0x00], |
343
|
115 |
|
0x14 => [0x80, 0x00, 0x80, 0x00], |
344
|
115 |
|
0x15 => [0x00, 0x80, 0x80, 0x00], |
345
|
115 |
|
0x16 => [0xC0, 0xC0, 0xC0, 0x00], |
346
|
115 |
|
0x17 => [0x80, 0x80, 0x80, 0x00], |
347
|
115 |
|
0x18 => [0x99, 0x99, 0xFF, 0x00], |
348
|
115 |
|
0x19 => [0x99, 0x33, 0x66, 0x00], |
349
|
115 |
|
0x1A => [0xFF, 0xFF, 0xCC, 0x00], |
350
|
115 |
|
0x1B => [0xCC, 0xFF, 0xFF, 0x00], |
351
|
115 |
|
0x1C => [0x66, 0x00, 0x66, 0x00], |
352
|
115 |
|
0x1D => [0xFF, 0x80, 0x80, 0x00], |
353
|
115 |
|
0x1E => [0x00, 0x66, 0xCC, 0x00], |
354
|
115 |
|
0x1F => [0xCC, 0xCC, 0xFF, 0x00], |
355
|
115 |
|
0x20 => [0x00, 0x00, 0x80, 0x00], |
356
|
115 |
|
0x21 => [0xFF, 0x00, 0xFF, 0x00], |
357
|
115 |
|
0x22 => [0xFF, 0xFF, 0x00, 0x00], |
358
|
115 |
|
0x23 => [0x00, 0xFF, 0xFF, 0x00], |
359
|
115 |
|
0x24 => [0x80, 0x00, 0x80, 0x00], |
360
|
115 |
|
0x25 => [0x80, 0x00, 0x00, 0x00], |
361
|
115 |
|
0x26 => [0x00, 0x80, 0x80, 0x00], |
362
|
115 |
|
0x27 => [0x00, 0x00, 0xFF, 0x00], |
363
|
115 |
|
0x28 => [0x00, 0xCC, 0xFF, 0x00], |
364
|
115 |
|
0x29 => [0xCC, 0xFF, 0xFF, 0x00], |
365
|
115 |
|
0x2A => [0xCC, 0xFF, 0xCC, 0x00], |
366
|
115 |
|
0x2B => [0xFF, 0xFF, 0x99, 0x00], |
367
|
115 |
|
0x2C => [0x99, 0xCC, 0xFF, 0x00], |
368
|
115 |
|
0x2D => [0xFF, 0x99, 0xCC, 0x00], |
369
|
115 |
|
0x2E => [0xCC, 0x99, 0xFF, 0x00], |
370
|
115 |
|
0x2F => [0xFF, 0xCC, 0x99, 0x00], |
371
|
115 |
|
0x30 => [0x33, 0x66, 0xFF, 0x00], |
372
|
115 |
|
0x31 => [0x33, 0xCC, 0xCC, 0x00], |
373
|
115 |
|
0x32 => [0x99, 0xCC, 0x00, 0x00], |
374
|
115 |
|
0x33 => [0xFF, 0xCC, 0x00, 0x00], |
375
|
115 |
|
0x34 => [0xFF, 0x99, 0x00, 0x00], |
376
|
115 |
|
0x35 => [0xFF, 0x66, 0x00, 0x00], |
377
|
115 |
|
0x36 => [0x66, 0x66, 0x99, 0x00], |
378
|
115 |
|
0x37 => [0x96, 0x96, 0x96, 0x00], |
379
|
115 |
|
0x38 => [0x00, 0x33, 0x66, 0x00], |
380
|
115 |
|
0x39 => [0x33, 0x99, 0x66, 0x00], |
381
|
115 |
|
0x3A => [0x00, 0x33, 0x00, 0x00], |
382
|
115 |
|
0x3B => [0x33, 0x33, 0x00, 0x00], |
383
|
115 |
|
0x3C => [0x99, 0x33, 0x00, 0x00], |
384
|
115 |
|
0x3D => [0x99, 0x33, 0x66, 0x00], |
385
|
115 |
|
0x3E => [0x33, 0x33, 0x99, 0x00], |
386
|
115 |
|
0x3F => [0x33, 0x33, 0x33, 0x00], |
387
|
115 |
|
]; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Assemble worksheets into a workbook and send the BIFF data to an OLE |
392
|
|
|
* storage. |
393
|
|
|
* |
394
|
|
|
* @param array $worksheetSizes The sizes in bytes of the binary worksheet streams |
395
|
|
|
* |
396
|
|
|
* @return string Binary data for workbook stream |
397
|
|
|
*/ |
398
|
113 |
|
public function writeWorkbook(array $worksheetSizes): string |
399
|
|
|
{ |
400
|
113 |
|
$this->worksheetSizes = $worksheetSizes; |
401
|
|
|
|
402
|
|
|
// Calculate the number of selected worksheet tabs and call the finalization |
403
|
|
|
// methods for each worksheet |
404
|
113 |
|
$total_worksheets = $this->spreadsheet->getSheetCount(); |
405
|
|
|
|
406
|
|
|
// Add part 1 of the Workbook globals, what goes before the SHEET records |
407
|
113 |
|
$this->storeBof(0x0005); |
408
|
113 |
|
$this->writeCodepage(); |
409
|
113 |
|
$this->writeWindow1(); |
410
|
|
|
|
411
|
113 |
|
$this->writeDateMode(); |
412
|
113 |
|
$this->writeAllFonts(); |
413
|
113 |
|
$this->writeAllNumberFormats(); |
414
|
113 |
|
$this->writeAllXfs(); |
415
|
113 |
|
$this->writeAllStyles(); |
416
|
113 |
|
$this->writePalette(); |
417
|
|
|
|
418
|
|
|
// Prepare part 3 of the workbook global stream, what goes after the SHEET records |
419
|
113 |
|
$part3 = ''; |
420
|
113 |
|
if ($this->countryCode !== -1) { |
421
|
|
|
$part3 .= $this->writeCountry(); |
422
|
|
|
} |
423
|
113 |
|
$part3 .= $this->writeRecalcId(); |
424
|
|
|
|
425
|
113 |
|
$part3 .= $this->writeSupbookInternal(); |
426
|
|
|
/* TODO: store external SUPBOOK records and XCT and CRN records |
427
|
|
|
in case of external references for BIFF8 */ |
428
|
113 |
|
$part3 .= $this->writeExternalsheetBiff8(); |
429
|
113 |
|
$part3 .= $this->writeAllDefinedNamesBiff8(); |
430
|
113 |
|
$part3 .= $this->writeMsoDrawingGroup(); |
431
|
113 |
|
$part3 .= $this->writeSharedStringsTable(); |
432
|
|
|
|
433
|
113 |
|
$part3 .= $this->writeEof(); |
434
|
|
|
|
435
|
|
|
// Add part 2 of the Workbook globals, the SHEET records |
436
|
113 |
|
$this->calcSheetOffsets(); |
437
|
113 |
|
for ($i = 0; $i < $total_worksheets; ++$i) { |
438
|
113 |
|
$this->writeBoundSheet($this->spreadsheet->getSheet($i), $this->worksheetOffsets[$i]); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
// Add part 3 of the Workbook globals |
442
|
113 |
|
$this->_data .= $part3; |
443
|
|
|
|
444
|
113 |
|
return $this->_data; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Calculate offsets for Worksheet BOF records. |
449
|
|
|
*/ |
450
|
113 |
|
private function calcSheetOffsets(): void |
451
|
|
|
{ |
452
|
113 |
|
$boundsheet_length = 10; // fixed length for a BOUNDSHEET record |
453
|
|
|
|
454
|
|
|
// size of Workbook globals part 1 + 3 |
455
|
113 |
|
$offset = $this->_datasize; |
456
|
|
|
|
457
|
|
|
// add size of Workbook globals part 2, the length of the SHEET records |
458
|
113 |
|
$total_worksheets = count($this->spreadsheet->getAllSheets()); |
459
|
113 |
|
foreach ($this->spreadsheet->getWorksheetIterator() as $sheet) { |
460
|
113 |
|
$offset += $boundsheet_length + strlen(StringHelper::UTF8toBIFF8UnicodeShort($sheet->getTitle())); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
// add the sizes of each of the Sheet substreams, respectively |
464
|
113 |
|
for ($i = 0; $i < $total_worksheets; ++$i) { |
465
|
113 |
|
$this->worksheetOffsets[$i] = $offset; |
466
|
113 |
|
$offset += $this->worksheetSizes[$i]; |
467
|
|
|
} |
468
|
|
|
//$this->biffSize = $offset; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Store the Excel FONT records. |
473
|
|
|
*/ |
474
|
113 |
|
private function writeAllFonts(): void |
475
|
|
|
{ |
476
|
113 |
|
foreach ($this->fontWriters as $fontWriter) { |
477
|
113 |
|
$this->append($fontWriter->writeFont()); |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Store user defined numerical formats i.e. FORMAT records. |
483
|
|
|
*/ |
484
|
113 |
|
private function writeAllNumberFormats(): void |
485
|
|
|
{ |
486
|
113 |
|
foreach ($this->numberFormats as $numberFormatIndex => $numberFormat) { |
487
|
22 |
|
$this->writeNumberFormat($numberFormat->getFormatCode(), $numberFormatIndex); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Write all XF records. |
493
|
|
|
*/ |
494
|
113 |
|
private function writeAllXfs(): void |
495
|
|
|
{ |
496
|
113 |
|
foreach ($this->xfWriters as $xfWriter) { |
497
|
113 |
|
$this->append($xfWriter->writeXf()); |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Write all STYLE records. |
503
|
|
|
*/ |
504
|
113 |
|
private function writeAllStyles(): void |
505
|
|
|
{ |
506
|
113 |
|
$this->writeStyle(); |
507
|
|
|
} |
508
|
|
|
|
509
|
9 |
|
private function parseDefinedNameValue(DefinedName $definedName): string |
510
|
|
|
{ |
511
|
9 |
|
$definedRange = $definedName->getValue(); |
512
|
9 |
|
$splitCount = Preg::matchAllWithOffsets( |
513
|
9 |
|
'/' . Calculation::CALCULATION_REGEXP_CELLREF . '/mui', |
514
|
9 |
|
$definedRange, |
515
|
9 |
|
$splitRanges |
516
|
9 |
|
); |
517
|
|
|
|
518
|
9 |
|
$lengths = array_map([StringHelper::class, 'strlenAllowNull'], array_column($splitRanges[0], 0)); |
519
|
9 |
|
$offsets = array_column($splitRanges[0], 1); |
520
|
|
|
|
521
|
9 |
|
$worksheets = $splitRanges[2]; |
522
|
9 |
|
$columns = $splitRanges[6]; |
523
|
9 |
|
$rows = $splitRanges[7]; |
524
|
|
|
|
525
|
9 |
|
while ($splitCount > 0) { |
526
|
9 |
|
--$splitCount; |
527
|
9 |
|
$length = $lengths[$splitCount]; |
528
|
9 |
|
$offset = $offsets[$splitCount]; |
529
|
9 |
|
$worksheet = $worksheets[$splitCount][0]; |
530
|
9 |
|
$column = $columns[$splitCount][0]; |
531
|
9 |
|
$row = $rows[$splitCount][0]; |
532
|
|
|
|
533
|
9 |
|
$newRange = ''; |
534
|
9 |
|
if (empty($worksheet)) { |
535
|
8 |
|
if (($offset === 0) || ($definedRange[$offset - 1] !== ':')) { |
536
|
|
|
// We should have a worksheet |
537
|
5 |
|
$worksheet = $definedName->getWorksheet() ? $definedName->getWorksheet()->getTitle() : null; |
538
|
|
|
} |
539
|
|
|
} else { |
540
|
4 |
|
$worksheet = str_replace("''", "'", trim($worksheet, "'")); |
541
|
|
|
} |
542
|
9 |
|
if (!empty($worksheet)) { |
543
|
9 |
|
$newRange = "'" . str_replace("'", "''", $worksheet) . "'!"; |
544
|
|
|
} |
545
|
|
|
|
546
|
9 |
|
if (!empty($column)) { |
547
|
9 |
|
$newRange .= "\${$column}"; |
548
|
|
|
} |
549
|
9 |
|
if (!empty($row)) { |
550
|
9 |
|
$newRange .= "\${$row}"; |
551
|
|
|
} |
552
|
|
|
|
553
|
9 |
|
$definedRange = substr($definedRange, 0, $offset) . $newRange . substr($definedRange, $offset + $length); |
554
|
|
|
} |
555
|
|
|
|
556
|
9 |
|
return $definedRange; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Writes all the DEFINEDNAME records (BIFF8). |
561
|
|
|
* So far this is only used for repeating rows/columns (print titles) and print areas. |
562
|
|
|
*/ |
563
|
113 |
|
private function writeAllDefinedNamesBiff8(): string |
564
|
|
|
{ |
565
|
113 |
|
$chunk = ''; |
566
|
|
|
|
567
|
|
|
// Named ranges |
568
|
113 |
|
$definedNames = $this->spreadsheet->getDefinedNames(); |
569
|
113 |
|
if (count($definedNames) > 0) { |
570
|
|
|
// Loop named ranges |
571
|
9 |
|
foreach ($definedNames as $definedName) { |
572
|
9 |
|
$range = $this->parseDefinedNameValue($definedName); |
573
|
|
|
|
574
|
|
|
// parse formula |
575
|
|
|
try { |
576
|
9 |
|
$this->parser->parse($range); |
577
|
9 |
|
$formulaData = $this->parser->toReversePolish(); |
578
|
|
|
|
579
|
|
|
// make sure tRef3d is of type tRef3dR (0x3A) |
580
|
8 |
|
if (isset($formulaData[0]) && ($formulaData[0] == "\x7A" || $formulaData[0] == "\x5A")) { |
581
|
7 |
|
$formulaData = "\x3A" . substr($formulaData, 1); |
582
|
|
|
} |
583
|
|
|
|
584
|
8 |
|
if ($definedName->getLocalOnly()) { |
585
|
|
|
// local scope |
586
|
1 |
|
$scopeWs = $definedName->getScope(); |
587
|
1 |
|
$scope = ($scopeWs === null) ? 0 : ($this->spreadsheet->getIndex($scopeWs) + 1); |
588
|
|
|
} else { |
589
|
|
|
// global scope |
590
|
8 |
|
$scope = 0; |
591
|
|
|
} |
592
|
8 |
|
$chunk .= $this->writeData($this->writeDefinedNameBiff8($definedName->getName(), $formulaData, $scope, false)); |
593
|
1 |
|
} catch (PhpSpreadsheetException) { |
594
|
|
|
// do nothing |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
// total number of sheets |
600
|
113 |
|
$total_worksheets = $this->spreadsheet->getSheetCount(); |
601
|
|
|
|
602
|
|
|
// write the print titles (repeating rows, columns), if any |
603
|
113 |
|
for ($i = 0; $i < $total_worksheets; ++$i) { |
604
|
113 |
|
$sheetSetup = $this->spreadsheet->getSheet($i)->getPageSetup(); |
605
|
|
|
// simultaneous repeatColumns repeatRows |
606
|
113 |
|
if ($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) { |
607
|
|
|
$repeat = $sheetSetup->getColumnsToRepeatAtLeft(); |
608
|
|
|
$colmin = Coordinate::columnIndexFromString($repeat[0]) - 1; |
609
|
|
|
$colmax = Coordinate::columnIndexFromString($repeat[1]) - 1; |
610
|
|
|
|
611
|
|
|
$repeat = $sheetSetup->getRowsToRepeatAtTop(); |
612
|
|
|
$rowmin = $repeat[0] - 1; |
613
|
|
|
$rowmax = $repeat[1] - 1; |
614
|
|
|
|
615
|
|
|
// construct formula data manually |
616
|
|
|
$formulaData = pack('Cv', 0x29, 0x17); // tMemFunc |
617
|
|
|
$formulaData .= pack('Cvvvvv', 0x3B, $i, 0, 65535, $colmin, $colmax); // tArea3d |
618
|
|
|
$formulaData .= pack('Cvvvvv', 0x3B, $i, $rowmin, $rowmax, 0, 255); // tArea3d |
619
|
|
|
$formulaData .= pack('C', 0x10); // tList |
620
|
|
|
|
621
|
|
|
// store the DEFINEDNAME record |
622
|
|
|
$chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x07), $formulaData, $i + 1, true)); |
623
|
113 |
|
} elseif ($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) { |
624
|
|
|
// (exclusive) either repeatColumns or repeatRows. |
625
|
|
|
// Columns to repeat |
626
|
2 |
|
if ($sheetSetup->isColumnsToRepeatAtLeftSet()) { |
627
|
|
|
$repeat = $sheetSetup->getColumnsToRepeatAtLeft(); |
628
|
|
|
$colmin = Coordinate::columnIndexFromString($repeat[0]) - 1; |
629
|
|
|
$colmax = Coordinate::columnIndexFromString($repeat[1]) - 1; |
630
|
|
|
} else { |
631
|
2 |
|
$colmin = 0; |
632
|
2 |
|
$colmax = 255; |
633
|
|
|
} |
634
|
|
|
// Rows to repeat |
635
|
2 |
|
if ($sheetSetup->isRowsToRepeatAtTopSet()) { |
636
|
2 |
|
$repeat = $sheetSetup->getRowsToRepeatAtTop(); |
637
|
2 |
|
$rowmin = $repeat[0] - 1; |
638
|
2 |
|
$rowmax = $repeat[1] - 1; |
639
|
|
|
} else { |
640
|
|
|
$rowmin = 0; |
641
|
|
|
$rowmax = 65535; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
// construct formula data manually because parser does not recognize absolute 3d cell references |
645
|
2 |
|
$formulaData = pack('Cvvvvv', 0x3B, $i, $rowmin, $rowmax, $colmin, $colmax); |
646
|
|
|
|
647
|
|
|
// store the DEFINEDNAME record |
648
|
2 |
|
$chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x07), $formulaData, $i + 1, true)); |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
// write the print areas, if any |
653
|
113 |
|
for ($i = 0; $i < $total_worksheets; ++$i) { |
654
|
113 |
|
$sheetSetup = $this->spreadsheet->getSheet($i)->getPageSetup(); |
655
|
113 |
|
if ($sheetSetup->isPrintAreaSet()) { |
656
|
|
|
// Print area, e.g. A3:J6,H1:X20 |
657
|
4 |
|
$printArea = Coordinate::splitRange($sheetSetup->getPrintArea()); |
658
|
4 |
|
$countPrintArea = count($printArea); |
659
|
|
|
|
660
|
4 |
|
$formulaData = ''; |
661
|
4 |
|
for ($j = 0; $j < $countPrintArea; ++$j) { |
662
|
4 |
|
$printAreaRect = $printArea[$j]; // e.g. A3:J6 |
663
|
4 |
|
$printAreaRect[0] = Coordinate::indexesFromString($printAreaRect[0]); |
664
|
|
|
/** @var string */ |
665
|
4 |
|
$printAreaRect1 = $printAreaRect[1]; |
666
|
4 |
|
$printAreaRect[1] = Coordinate::indexesFromString($printAreaRect1); |
667
|
|
|
|
668
|
4 |
|
$print_rowmin = $printAreaRect[0][1] - 1; |
669
|
4 |
|
$print_rowmax = $printAreaRect[1][1] - 1; |
670
|
4 |
|
$print_colmin = $printAreaRect[0][0] - 1; |
671
|
4 |
|
$print_colmax = $printAreaRect[1][0] - 1; |
672
|
|
|
|
673
|
|
|
// construct formula data manually because parser does not recognize absolute 3d cell references |
674
|
4 |
|
$formulaData .= pack('Cvvvvv', 0x3B, $i, $print_rowmin, $print_rowmax, $print_colmin, $print_colmax); |
675
|
|
|
|
676
|
4 |
|
if ($j > 0) { |
677
|
1 |
|
$formulaData .= pack('C', 0x10); // list operator token ',' |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
// store the DEFINEDNAME record |
682
|
4 |
|
$chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x06), $formulaData, $i + 1, true)); |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
// write autofilters, if any |
687
|
113 |
|
for ($i = 0; $i < $total_worksheets; ++$i) { |
688
|
113 |
|
$sheetAutoFilter = $this->spreadsheet->getSheet($i)->getAutoFilter(); |
689
|
113 |
|
$autoFilterRange = $sheetAutoFilter->getRange(); |
690
|
113 |
|
if (!empty($autoFilterRange)) { |
691
|
3 |
|
$rangeBounds = Coordinate::rangeBoundaries($autoFilterRange); |
692
|
|
|
|
693
|
|
|
//Autofilter built in name |
694
|
3 |
|
$name = pack('C', 0x0D); |
695
|
|
|
|
696
|
3 |
|
$chunk .= $this->writeData($this->writeShortNameBiff8($name, $i + 1, $rangeBounds, true)); |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
|
700
|
113 |
|
return $chunk; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Write a DEFINEDNAME record for BIFF8 using explicit binary formula data. |
705
|
|
|
* |
706
|
|
|
* @param string $name The name in UTF-8 |
707
|
|
|
* @param string $formulaData The binary formula data |
708
|
|
|
* @param int $sheetIndex 1-based sheet index the defined name applies to. 0 = global |
709
|
|
|
* @param bool $isBuiltIn Built-in name? |
710
|
|
|
* |
711
|
|
|
* @return string Complete binary record data |
712
|
|
|
*/ |
713
|
12 |
|
private function writeDefinedNameBiff8(string $name, string $formulaData, int $sheetIndex = 0, bool $isBuiltIn = false): string |
714
|
|
|
{ |
715
|
12 |
|
$record = 0x0018; |
716
|
|
|
|
717
|
|
|
// option flags |
718
|
12 |
|
$options = $isBuiltIn ? 0x20 : 0x00; |
719
|
|
|
|
720
|
|
|
// length of the name, character count |
721
|
12 |
|
$nlen = StringHelper::countCharacters($name); |
722
|
|
|
|
723
|
|
|
// name with stripped length field |
724
|
12 |
|
$name = substr(StringHelper::UTF8toBIFF8UnicodeLong($name), 2); |
725
|
|
|
|
726
|
|
|
// size of the formula (in bytes) |
727
|
12 |
|
$sz = strlen($formulaData); |
728
|
|
|
|
729
|
|
|
// combine the parts |
730
|
12 |
|
$data = pack('vCCvvvCCCC', $options, 0, $nlen, $sz, 0, $sheetIndex, 0, 0, 0, 0) |
731
|
12 |
|
. $name . $formulaData; |
732
|
12 |
|
$length = strlen($data); |
733
|
|
|
|
734
|
12 |
|
$header = pack('vv', $record, $length); |
735
|
|
|
|
736
|
12 |
|
return $header . $data; |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* Write a short NAME record. |
741
|
|
|
* |
742
|
|
|
* @param int $sheetIndex 1-based sheet index the defined name applies to. 0 = global |
743
|
|
|
* @param int[][] $rangeBounds range boundaries |
744
|
|
|
* |
745
|
|
|
* @return string Complete binary record data |
746
|
|
|
* */ |
747
|
3 |
|
private function writeShortNameBiff8(string $name, int $sheetIndex, array $rangeBounds, bool $isHidden = false): string |
748
|
|
|
{ |
749
|
3 |
|
$record = 0x0018; |
750
|
|
|
|
751
|
|
|
// option flags |
752
|
3 |
|
$options = ($isHidden ? 0x21 : 0x00); |
753
|
|
|
|
754
|
3 |
|
$extra = pack( |
755
|
3 |
|
'Cvvvvv', |
756
|
3 |
|
0x3B, |
757
|
3 |
|
$sheetIndex - 1, |
758
|
3 |
|
$rangeBounds[0][1] - 1, |
759
|
3 |
|
$rangeBounds[1][1] - 1, |
760
|
3 |
|
$rangeBounds[0][0] - 1, |
761
|
3 |
|
$rangeBounds[1][0] - 1 |
762
|
3 |
|
); |
763
|
|
|
|
764
|
|
|
// size of the formula (in bytes) |
765
|
3 |
|
$sz = strlen($extra); |
766
|
|
|
|
767
|
|
|
// combine the parts |
768
|
3 |
|
$data = pack('vCCvvvCCCCC', $options, 0, 1, $sz, 0, $sheetIndex, 0, 0, 0, 0, 0) |
769
|
3 |
|
. $name . $extra; |
770
|
3 |
|
$length = strlen($data); |
771
|
|
|
|
772
|
3 |
|
$header = pack('vv', $record, $length); |
773
|
|
|
|
774
|
3 |
|
return $header . $data; |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
/** |
778
|
|
|
* Stores the CODEPAGE biff record. |
779
|
|
|
*/ |
780
|
113 |
|
private function writeCodepage(): void |
781
|
|
|
{ |
782
|
113 |
|
$record = 0x0042; // Record identifier |
783
|
113 |
|
$length = 0x0002; // Number of bytes to follow |
784
|
113 |
|
$cv = $this->codepage; // The code page |
785
|
|
|
|
786
|
113 |
|
$header = pack('vv', $record, $length); |
787
|
113 |
|
$data = pack('v', $cv); |
788
|
|
|
|
789
|
113 |
|
$this->append($header . $data); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Write Excel BIFF WINDOW1 record. |
794
|
|
|
*/ |
795
|
113 |
|
private function writeWindow1(): void |
796
|
|
|
{ |
797
|
113 |
|
$record = 0x003D; // Record identifier |
798
|
113 |
|
$length = 0x0012; // Number of bytes to follow |
799
|
|
|
|
800
|
113 |
|
$xWn = 0x0000; // Horizontal position of window |
801
|
113 |
|
$yWn = 0x0000; // Vertical position of window |
802
|
113 |
|
$dxWn = 0x25BC; // Width of window |
803
|
113 |
|
$dyWn = 0x1572; // Height of window |
804
|
|
|
|
805
|
113 |
|
$grbit = 0x0038; // Option flags |
806
|
|
|
|
807
|
|
|
// not supported by PhpSpreadsheet, so there is only one selected sheet, the active |
808
|
113 |
|
$ctabsel = 1; // Number of workbook tabs selected |
809
|
|
|
|
810
|
113 |
|
$wTabRatio = 0x0258; // Tab to scrollbar ratio |
811
|
|
|
|
812
|
|
|
// not supported by PhpSpreadsheet, set to 0 |
813
|
113 |
|
$itabFirst = 0; // 1st displayed worksheet |
814
|
113 |
|
$itabCur = $this->spreadsheet->getActiveSheetIndex(); // Active worksheet |
815
|
|
|
|
816
|
113 |
|
$header = pack('vv', $record, $length); |
817
|
113 |
|
$data = pack('vvvvvvvvv', $xWn, $yWn, $dxWn, $dyWn, $grbit, $itabCur, $itabFirst, $ctabsel, $wTabRatio); |
818
|
113 |
|
$this->append($header . $data); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Writes Excel BIFF BOUNDSHEET record. |
823
|
|
|
* |
824
|
|
|
* @param int $offset Location of worksheet BOF |
825
|
|
|
*/ |
826
|
113 |
|
private function writeBoundSheet(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet, int $offset): void |
827
|
|
|
{ |
828
|
113 |
|
$sheetname = $sheet->getTitle(); |
829
|
113 |
|
$record = 0x0085; // Record identifier |
830
|
113 |
|
$ss = match ($sheet->getSheetState()) { |
831
|
113 |
|
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::SHEETSTATE_VISIBLE => 0x00, |
832
|
1 |
|
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::SHEETSTATE_HIDDEN => 0x01, |
833
|
1 |
|
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::SHEETSTATE_VERYHIDDEN => 0x02, |
834
|
|
|
default => 0x00, |
835
|
113 |
|
}; |
836
|
|
|
|
837
|
|
|
// sheet type |
838
|
113 |
|
$st = 0x00; |
839
|
|
|
|
840
|
|
|
//$grbit = 0x0000; // Visibility and sheet type |
841
|
|
|
|
842
|
113 |
|
$data = pack('VCC', $offset, $ss, $st); |
843
|
113 |
|
$data .= StringHelper::UTF8toBIFF8UnicodeShort($sheetname); |
844
|
|
|
|
845
|
113 |
|
$length = strlen($data); |
846
|
113 |
|
$header = pack('vv', $record, $length); |
847
|
113 |
|
$this->append($header . $data); |
848
|
|
|
} |
849
|
|
|
|
850
|
|
|
/** |
851
|
|
|
* Write Internal SUPBOOK record. |
852
|
|
|
*/ |
853
|
113 |
|
private function writeSupbookInternal(): string |
854
|
|
|
{ |
855
|
113 |
|
$record = 0x01AE; // Record identifier |
856
|
113 |
|
$length = 0x0004; // Bytes to follow |
857
|
|
|
|
858
|
113 |
|
$header = pack('vv', $record, $length); |
859
|
113 |
|
$data = pack('vv', $this->spreadsheet->getSheetCount(), 0x0401); |
860
|
|
|
|
861
|
113 |
|
return $this->writeData($header . $data); |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
/** |
865
|
|
|
* Writes the Excel BIFF EXTERNSHEET record. These references are used by |
866
|
|
|
* formulas. |
867
|
|
|
*/ |
868
|
113 |
|
private function writeExternalsheetBiff8(): string |
869
|
|
|
{ |
870
|
113 |
|
$totalReferences = count($this->parser->references); |
871
|
113 |
|
$record = 0x0017; // Record identifier |
872
|
113 |
|
$length = 2 + 6 * $totalReferences; // Number of bytes to follow |
873
|
|
|
|
874
|
|
|
//$supbook_index = 0; // FIXME: only using internal SUPBOOK record |
875
|
113 |
|
$header = pack('vv', $record, $length); |
876
|
113 |
|
$data = pack('v', $totalReferences); |
877
|
113 |
|
for ($i = 0; $i < $totalReferences; ++$i) { |
878
|
113 |
|
$data .= $this->parser->references[$i]; |
879
|
|
|
} |
880
|
|
|
|
881
|
113 |
|
return $this->writeData($header . $data); |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
/** |
885
|
|
|
* Write Excel BIFF STYLE records. |
886
|
|
|
*/ |
887
|
113 |
|
private function writeStyle(): void |
888
|
|
|
{ |
889
|
113 |
|
$record = 0x0293; // Record identifier |
890
|
113 |
|
$length = 0x0004; // Bytes to follow |
891
|
|
|
|
892
|
113 |
|
$ixfe = 0x8000; // Index to cell style XF |
893
|
113 |
|
$BuiltIn = 0x00; // Built-in style |
894
|
113 |
|
$iLevel = 0xFF; // Outline style level |
895
|
|
|
|
896
|
113 |
|
$header = pack('vv', $record, $length); |
897
|
113 |
|
$data = pack('vCC', $ixfe, $BuiltIn, $iLevel); |
898
|
113 |
|
$this->append($header . $data); |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
/** |
902
|
|
|
* Writes Excel FORMAT record for non "built-in" numerical formats. |
903
|
|
|
* |
904
|
|
|
* @param string $format Custom format string |
905
|
|
|
* @param int $ifmt Format index code |
906
|
|
|
*/ |
907
|
22 |
|
private function writeNumberFormat(string $format, int $ifmt): void |
908
|
|
|
{ |
909
|
22 |
|
$record = 0x041E; // Record identifier |
910
|
|
|
|
911
|
22 |
|
$numberFormatString = StringHelper::UTF8toBIFF8UnicodeLong($format); |
912
|
22 |
|
$length = 2 + strlen($numberFormatString); // Number of bytes to follow |
913
|
|
|
|
914
|
22 |
|
$header = pack('vv', $record, $length); |
915
|
22 |
|
$data = pack('v', $ifmt) . $numberFormatString; |
916
|
22 |
|
$this->append($header . $data); |
917
|
|
|
} |
918
|
|
|
|
919
|
|
|
/** |
920
|
|
|
* Write DATEMODE record to indicate the date system in use (1904 or 1900). |
921
|
|
|
*/ |
922
|
113 |
|
private function writeDateMode(): void |
923
|
|
|
{ |
924
|
113 |
|
$record = 0x0022; // Record identifier |
925
|
113 |
|
$length = 0x0002; // Bytes to follow |
926
|
|
|
|
927
|
113 |
|
$f1904 = ($this->spreadsheet->getExcelCalendar() === Date::CALENDAR_MAC_1904) |
928
|
|
|
? 1 // Flag for 1904 date system |
929
|
113 |
|
: 0; // Flag for 1900 date system |
930
|
|
|
|
931
|
113 |
|
$header = pack('vv', $record, $length); |
932
|
113 |
|
$data = pack('v', $f1904); |
933
|
113 |
|
$this->append($header . $data); |
934
|
|
|
} |
935
|
|
|
|
936
|
|
|
/** |
937
|
|
|
* Stores the COUNTRY record for localization. |
938
|
|
|
*/ |
939
|
|
|
private function writeCountry(): string |
940
|
|
|
{ |
941
|
|
|
$record = 0x008C; // Record identifier |
942
|
|
|
$length = 4; // Number of bytes to follow |
943
|
|
|
|
944
|
|
|
$header = pack('vv', $record, $length); |
945
|
|
|
// using the same country code always for simplicity |
946
|
|
|
$data = pack('vv', $this->countryCode, $this->countryCode); |
947
|
|
|
|
948
|
|
|
return $this->writeData($header . $data); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Write the RECALCID record. |
953
|
|
|
*/ |
954
|
113 |
|
private function writeRecalcId(): string |
955
|
|
|
{ |
956
|
113 |
|
$record = 0x01C1; // Record identifier |
957
|
113 |
|
$length = 8; // Number of bytes to follow |
958
|
|
|
|
959
|
113 |
|
$header = pack('vv', $record, $length); |
960
|
|
|
|
961
|
|
|
// by inspection of real Excel files, MS Office Excel 2007 writes this |
962
|
113 |
|
$data = pack('VV', 0x000001C1, 0x00001E667); |
963
|
|
|
|
964
|
113 |
|
return $this->writeData($header . $data); |
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
/** |
968
|
|
|
* Stores the PALETTE biff record. |
969
|
|
|
*/ |
970
|
113 |
|
private function writePalette(): void |
971
|
|
|
{ |
972
|
113 |
|
$aref = $this->palette; |
973
|
|
|
|
974
|
113 |
|
$record = 0x0092; // Record identifier |
975
|
113 |
|
$length = 2 + 4 * count($aref); // Number of bytes to follow |
976
|
113 |
|
$ccv = count($aref); // Number of RGB values to follow |
977
|
113 |
|
$data = ''; // The RGB data |
978
|
|
|
|
979
|
|
|
// Pack the RGB data |
980
|
113 |
|
foreach ($aref as $color) { |
981
|
113 |
|
foreach ($color as $byte) { |
982
|
113 |
|
$data .= pack('C', $byte); |
983
|
|
|
} |
984
|
|
|
} |
985
|
|
|
|
986
|
113 |
|
$header = pack('vvv', $record, $length, $ccv); |
987
|
113 |
|
$this->append($header . $data); |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
/** |
991
|
|
|
* Handling of the SST continue blocks is complicated by the need to include an |
992
|
|
|
* additional continuation byte depending on whether the string is split between |
993
|
|
|
* blocks or whether it starts at the beginning of the block. (There are also |
994
|
|
|
* additional complications that will arise later when/if Rich Strings are |
995
|
|
|
* supported). |
996
|
|
|
* |
997
|
|
|
* The Excel documentation says that the SST record should be followed by an |
998
|
|
|
* EXTSST record. The EXTSST record is a hash table that is used to optimise |
999
|
|
|
* access to SST. However, despite the documentation it doesn't seem to be |
1000
|
|
|
* required so we will ignore it. |
1001
|
|
|
* |
1002
|
|
|
* @return string Binary data |
1003
|
|
|
*/ |
1004
|
113 |
|
private function writeSharedStringsTable(): string |
1005
|
|
|
{ |
1006
|
|
|
// maximum size of record data (excluding record header) |
1007
|
113 |
|
$continue_limit = 8224; |
1008
|
|
|
|
1009
|
|
|
// initialize array of record data blocks |
1010
|
113 |
|
$recordDatas = []; |
1011
|
|
|
|
1012
|
|
|
// start SST record data block with total number of strings, total number of unique strings |
1013
|
113 |
|
$recordData = pack('VV', $this->stringTotal, $this->stringUnique); |
1014
|
|
|
|
1015
|
|
|
// loop through all (unique) strings in shared strings table |
1016
|
113 |
|
foreach (array_keys($this->stringTable) as $string) { |
1017
|
|
|
// here $string is a BIFF8 encoded string |
1018
|
|
|
|
1019
|
|
|
// length = character count |
1020
|
75 |
|
$headerinfo = unpack('vlength/Cencoding', $string); |
1021
|
|
|
|
1022
|
|
|
// currently, this is always 1 = uncompressed |
1023
|
75 |
|
$encoding = $headerinfo['encoding'] ?? 1; |
1024
|
|
|
|
1025
|
|
|
// initialize finished writing current $string |
1026
|
75 |
|
$finished = false; |
1027
|
|
|
|
1028
|
75 |
|
while ($finished === false) { |
1029
|
|
|
// normally, there will be only one cycle, but if string cannot immediately be written as is |
1030
|
|
|
// there will be need for more than one cylcle, if string longer than one record data block, there |
1031
|
|
|
// may be need for even more cycles |
1032
|
|
|
|
1033
|
75 |
|
if (strlen($recordData) + strlen($string) <= $continue_limit) { |
1034
|
|
|
// then we can write the string (or remainder of string) without any problems |
1035
|
75 |
|
$recordData .= $string; |
1036
|
|
|
|
1037
|
75 |
|
if (strlen($recordData) + strlen($string) == $continue_limit) { |
1038
|
|
|
// we close the record data block, and initialize a new one |
1039
|
|
|
$recordDatas[] = $recordData; |
1040
|
|
|
$recordData = ''; |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
// we are finished writing this string |
1044
|
75 |
|
$finished = true; |
1045
|
|
|
} else { |
1046
|
|
|
// special treatment writing the string (or remainder of the string) |
1047
|
|
|
// If the string is very long it may need to be written in more than one CONTINUE record. |
1048
|
|
|
|
1049
|
|
|
// check how many bytes more there is room for in the current record |
1050
|
1 |
|
$space_remaining = $continue_limit - strlen($recordData); |
1051
|
|
|
|
1052
|
|
|
// minimum space needed |
1053
|
|
|
// uncompressed: 2 byte string length length field + 1 byte option flags + 2 byte character |
1054
|
|
|
// compressed: 2 byte string length length field + 1 byte option flags + 1 byte character |
1055
|
1 |
|
$min_space_needed = ($encoding == 1) ? 5 : 4; |
1056
|
|
|
|
1057
|
|
|
// We have two cases |
1058
|
|
|
// 1. space remaining is less than minimum space needed |
1059
|
|
|
// here we must waste the space remaining and move to next record data block |
1060
|
|
|
// 2. space remaining is greater than or equal to minimum space needed |
1061
|
|
|
// here we write as much as we can in the current block, then move to next record data block |
1062
|
|
|
|
1063
|
1 |
|
if ($space_remaining < $min_space_needed) { |
1064
|
|
|
// 1. space remaining is less than minimum space needed. |
1065
|
|
|
// we close the block, store the block data |
1066
|
|
|
$recordDatas[] = $recordData; |
1067
|
|
|
|
1068
|
|
|
// and start new record data block where we start writing the string |
1069
|
|
|
$recordData = ''; |
1070
|
|
|
} else { |
1071
|
|
|
// 2. space remaining is greater than or equal to minimum space needed. |
1072
|
|
|
// initialize effective remaining space, for Unicode strings this may need to be reduced by 1, see below |
1073
|
1 |
|
$effective_space_remaining = $space_remaining; |
1074
|
|
|
|
1075
|
|
|
// for uncompressed strings, sometimes effective space remaining is reduced by 1 |
1076
|
1 |
|
if ($encoding == 1 && (strlen($string) - $space_remaining) % 2 == 1) { |
1077
|
1 |
|
--$effective_space_remaining; |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
// one block fininshed, store the block data |
1081
|
1 |
|
$recordData .= substr($string, 0, $effective_space_remaining); |
1082
|
|
|
|
1083
|
1 |
|
$string = substr($string, $effective_space_remaining); // for next cycle in while loop |
1084
|
1 |
|
$recordDatas[] = $recordData; |
1085
|
|
|
|
1086
|
|
|
// start new record data block with the repeated option flags |
1087
|
1 |
|
$recordData = pack('C', $encoding); |
1088
|
|
|
} |
1089
|
|
|
} |
1090
|
|
|
} |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
// Store the last record data block unless it is empty |
1094
|
|
|
// if there was no need for any continue records, this will be the for SST record data block itself |
1095
|
113 |
|
if ($recordData !== '') { |
1096
|
113 |
|
$recordDatas[] = $recordData; |
1097
|
|
|
} |
1098
|
|
|
|
1099
|
|
|
// combine into one chunk with all the blocks SST, CONTINUE,... |
1100
|
113 |
|
$chunk = ''; |
1101
|
113 |
|
foreach ($recordDatas as $i => $recordData) { |
1102
|
|
|
// first block should have the SST record header, remaing should have CONTINUE header |
1103
|
113 |
|
$record = ($i == 0) ? 0x00FC : 0x003C; |
1104
|
|
|
|
1105
|
113 |
|
$header = pack('vv', $record, strlen($recordData)); |
1106
|
113 |
|
$data = $header . $recordData; |
1107
|
|
|
|
1108
|
113 |
|
$chunk .= $this->writeData($data); |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
113 |
|
return $chunk; |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
/** |
1115
|
|
|
* Writes the MSODRAWINGGROUP record if needed. Possibly split using CONTINUE records. |
1116
|
|
|
*/ |
1117
|
113 |
|
private function writeMsoDrawingGroup(): string |
1118
|
|
|
{ |
1119
|
|
|
// write the Escher stream if necessary |
1120
|
113 |
|
if (isset($this->escher)) { |
1121
|
15 |
|
$writer = new Escher($this->escher); |
1122
|
15 |
|
$data = $writer->close(); |
1123
|
|
|
|
1124
|
15 |
|
$record = 0x00EB; |
1125
|
15 |
|
$length = strlen($data); |
1126
|
15 |
|
$header = pack('vv', $record, $length); |
1127
|
|
|
|
1128
|
15 |
|
return $this->writeData($header . $data); |
1129
|
|
|
} |
1130
|
|
|
|
1131
|
98 |
|
return ''; |
1132
|
|
|
} |
1133
|
|
|
|
1134
|
|
|
/** |
1135
|
|
|
* Get Escher object. |
1136
|
|
|
*/ |
1137
|
|
|
public function getEscher(): ?\PhpOffice\PhpSpreadsheet\Shared\Escher |
1138
|
|
|
{ |
1139
|
|
|
return $this->escher; |
1140
|
|
|
} |
1141
|
|
|
|
1142
|
|
|
/** |
1143
|
|
|
* Set Escher object. |
1144
|
|
|
*/ |
1145
|
15 |
|
public function setEscher(?\PhpOffice\PhpSpreadsheet\Shared\Escher $escher): void |
1146
|
|
|
{ |
1147
|
15 |
|
$this->escher = $escher; |
1148
|
|
|
} |
1149
|
|
|
} |
1150
|
|
|
|