| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
|
|
|
10 |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\DefinedNames as DefinedNamesWriter;
|
|
|
11 |
|
|
|
12 |
class Workbook extends WriterPart
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* Write workbook to XML format.
|
|
|
16 |
*
|
|
|
17 |
* @param bool $preCalculateFormulas If true, formulas will be calculated before writing
|
|
|
18 |
* @param ?bool $forceFullCalc If null, !$preCalculateFormulas
|
|
|
19 |
*
|
|
|
20 |
* @return string XML Output
|
|
|
21 |
*/
|
|
|
22 |
public function writeWorkbook(Spreadsheet $spreadsheet, bool $preCalculateFormulas = false, ?bool $forceFullCalc = null): string
|
|
|
23 |
{
|
|
|
24 |
// Create XML writer
|
|
|
25 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
26 |
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
27 |
} else {
|
|
|
28 |
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
// XML header
|
|
|
32 |
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
33 |
|
|
|
34 |
// workbook
|
|
|
35 |
$objWriter->startElement('workbook');
|
|
|
36 |
$objWriter->writeAttribute('xml:space', 'preserve');
|
|
|
37 |
$objWriter->writeAttribute('xmlns', Namespaces::MAIN);
|
|
|
38 |
$objWriter->writeAttribute('xmlns:r', Namespaces::SCHEMA_OFFICE_DOCUMENT);
|
|
|
39 |
|
|
|
40 |
// fileVersion
|
|
|
41 |
$this->writeFileVersion($objWriter);
|
|
|
42 |
|
|
|
43 |
// workbookPr
|
|
|
44 |
$this->writeWorkbookPr($objWriter, $spreadsheet);
|
|
|
45 |
|
|
|
46 |
// workbookProtection
|
|
|
47 |
$this->writeWorkbookProtection($objWriter, $spreadsheet);
|
|
|
48 |
|
|
|
49 |
// bookViews
|
|
|
50 |
if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
|
|
|
51 |
$this->writeBookViews($objWriter, $spreadsheet);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// sheets
|
|
|
55 |
$this->writeSheets($objWriter, $spreadsheet);
|
|
|
56 |
|
|
|
57 |
// definedNames
|
|
|
58 |
(new DefinedNamesWriter($objWriter, $spreadsheet))->write();
|
|
|
59 |
|
|
|
60 |
// calcPr
|
|
|
61 |
$this->writeCalcPr($objWriter, $preCalculateFormulas, $forceFullCalc);
|
|
|
62 |
|
|
|
63 |
$objWriter->endElement();
|
|
|
64 |
|
|
|
65 |
// Return
|
|
|
66 |
return $objWriter->getData();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Write file version.
|
|
|
71 |
*/
|
|
|
72 |
private function writeFileVersion(XMLWriter $objWriter): void
|
|
|
73 |
{
|
|
|
74 |
$objWriter->startElement('fileVersion');
|
|
|
75 |
$objWriter->writeAttribute('appName', 'xl');
|
|
|
76 |
$objWriter->writeAttribute('lastEdited', '4');
|
|
|
77 |
$objWriter->writeAttribute('lowestEdited', '4');
|
|
|
78 |
$objWriter->writeAttribute('rupBuild', '4505');
|
|
|
79 |
$objWriter->endElement();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Write WorkbookPr.
|
|
|
84 |
*/
|
|
|
85 |
private function writeWorkbookPr(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
|
|
|
86 |
{
|
|
|
87 |
$objWriter->startElement('workbookPr');
|
|
|
88 |
|
|
|
89 |
if ($spreadsheet->getExcelCalendar() === Date::CALENDAR_MAC_1904) {
|
|
|
90 |
$objWriter->writeAttribute('date1904', '1');
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$objWriter->writeAttribute('codeName', 'ThisWorkbook');
|
|
|
94 |
|
|
|
95 |
$objWriter->endElement();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Write BookViews.
|
|
|
100 |
*/
|
|
|
101 |
private function writeBookViews(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
|
|
|
102 |
{
|
|
|
103 |
// bookViews
|
|
|
104 |
$objWriter->startElement('bookViews');
|
|
|
105 |
|
|
|
106 |
// workbookView
|
|
|
107 |
$objWriter->startElement('workbookView');
|
|
|
108 |
|
|
|
109 |
$objWriter->writeAttribute('activeTab', (string) $spreadsheet->getActiveSheetIndex());
|
|
|
110 |
$objWriter->writeAttribute('autoFilterDateGrouping', ($spreadsheet->getAutoFilterDateGrouping() ? 'true' : 'false'));
|
|
|
111 |
$objWriter->writeAttribute('firstSheet', (string) $spreadsheet->getFirstSheetIndex());
|
|
|
112 |
$objWriter->writeAttribute('minimized', ($spreadsheet->getMinimized() ? 'true' : 'false'));
|
|
|
113 |
$objWriter->writeAttribute('showHorizontalScroll', ($spreadsheet->getShowHorizontalScroll() ? 'true' : 'false'));
|
|
|
114 |
$objWriter->writeAttribute('showSheetTabs', ($spreadsheet->getShowSheetTabs() ? 'true' : 'false'));
|
|
|
115 |
$objWriter->writeAttribute('showVerticalScroll', ($spreadsheet->getShowVerticalScroll() ? 'true' : 'false'));
|
|
|
116 |
$objWriter->writeAttribute('tabRatio', (string) $spreadsheet->getTabRatio());
|
|
|
117 |
$objWriter->writeAttribute('visibility', $spreadsheet->getVisibility());
|
|
|
118 |
|
|
|
119 |
$objWriter->endElement();
|
|
|
120 |
|
|
|
121 |
$objWriter->endElement();
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Write WorkbookProtection.
|
|
|
126 |
*/
|
|
|
127 |
private function writeWorkbookProtection(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
|
|
|
128 |
{
|
|
|
129 |
if ($spreadsheet->getSecurity()->isSecurityEnabled()) {
|
|
|
130 |
$objWriter->startElement('workbookProtection');
|
|
|
131 |
$objWriter->writeAttribute('lockRevision', ($spreadsheet->getSecurity()->getLockRevision() ? 'true' : 'false'));
|
|
|
132 |
$objWriter->writeAttribute('lockStructure', ($spreadsheet->getSecurity()->getLockStructure() ? 'true' : 'false'));
|
|
|
133 |
$objWriter->writeAttribute('lockWindows', ($spreadsheet->getSecurity()->getLockWindows() ? 'true' : 'false'));
|
|
|
134 |
|
|
|
135 |
if ($spreadsheet->getSecurity()->getRevisionsPassword() != '') {
|
|
|
136 |
$objWriter->writeAttribute('revisionsPassword', $spreadsheet->getSecurity()->getRevisionsPassword());
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
if ($spreadsheet->getSecurity()->getWorkbookPassword() != '') {
|
|
|
140 |
$objWriter->writeAttribute('workbookPassword', $spreadsheet->getSecurity()->getWorkbookPassword());
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$objWriter->endElement();
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Write calcPr.
|
|
|
149 |
*
|
|
|
150 |
* @param bool $preCalculateFormulas If true, formulas will be calculated before writing
|
|
|
151 |
*/
|
|
|
152 |
private function writeCalcPr(XMLWriter $objWriter, bool $preCalculateFormulas, ?bool $forceFullCalc): void
|
|
|
153 |
{
|
|
|
154 |
$objWriter->startElement('calcPr');
|
|
|
155 |
|
|
|
156 |
// Set the calcid to a higher value than Excel itself will use, otherwise Excel will always recalc
|
|
|
157 |
// If MS Excel does do a recalc, then users opening a file in MS Excel will be prompted to save on exit
|
|
|
158 |
// because the file has changed
|
|
|
159 |
$objWriter->writeAttribute('calcId', '999999');
|
|
|
160 |
$objWriter->writeAttribute('calcMode', 'auto');
|
|
|
161 |
// fullCalcOnLoad isn't needed if we will calculate before writing
|
|
|
162 |
$objWriter->writeAttribute('calcCompleted', ($preCalculateFormulas) ? '1' : '0');
|
|
|
163 |
$objWriter->writeAttribute('fullCalcOnLoad', ($preCalculateFormulas) ? '0' : '1');
|
|
|
164 |
if ($forceFullCalc === null) {
|
|
|
165 |
$objWriter->writeAttribute('forceFullCalc', $preCalculateFormulas ? '0' : '1');
|
|
|
166 |
} else {
|
|
|
167 |
$objWriter->writeAttribute('forceFullCalc', $forceFullCalc ? '1' : '0');
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
$objWriter->endElement();
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Write sheets.
|
|
|
175 |
*/
|
|
|
176 |
private function writeSheets(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
|
|
|
177 |
{
|
|
|
178 |
// Write sheets
|
|
|
179 |
$objWriter->startElement('sheets');
|
|
|
180 |
$sheetCount = $spreadsheet->getSheetCount();
|
|
|
181 |
for ($i = 0; $i < $sheetCount; ++$i) {
|
|
|
182 |
// sheet
|
|
|
183 |
$this->writeSheet(
|
|
|
184 |
$objWriter,
|
|
|
185 |
$spreadsheet->getSheet($i)->getTitle(),
|
|
|
186 |
($i + 1),
|
|
|
187 |
($i + 1 + 3),
|
|
|
188 |
$spreadsheet->getSheet($i)->getSheetState()
|
|
|
189 |
);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
$objWriter->endElement();
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Write sheet.
|
|
|
197 |
*
|
|
|
198 |
* @param string $worksheetName Sheet name
|
|
|
199 |
* @param int $worksheetId Sheet id
|
|
|
200 |
* @param int $relId Relationship ID
|
|
|
201 |
* @param string $sheetState Sheet state (visible, hidden, veryHidden)
|
|
|
202 |
*/
|
|
|
203 |
private function writeSheet(XMLWriter $objWriter, string $worksheetName, int $worksheetId = 1, int $relId = 1, string $sheetState = 'visible'): void
|
|
|
204 |
{
|
|
|
205 |
if ($worksheetName != '') {
|
|
|
206 |
// Write sheet
|
|
|
207 |
$objWriter->startElement('sheet');
|
|
|
208 |
$objWriter->writeAttribute('name', $worksheetName);
|
|
|
209 |
$objWriter->writeAttribute('sheetId', (string) $worksheetId);
|
|
|
210 |
if ($sheetState !== 'visible' && $sheetState != '') {
|
|
|
211 |
$objWriter->writeAttribute('state', $sheetState);
|
|
|
212 |
}
|
|
|
213 |
$objWriter->writeAttribute('r:id', 'rId' . $relId);
|
|
|
214 |
$objWriter->endElement();
|
|
|
215 |
} else {
|
|
|
216 |
throw new WriterException('Invalid parameters passed.');
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
}
|