ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Workbook.php
Go to the documentation of this file.
1 <?php
37 {
46  public function writeWorkbook(PHPExcel $pPHPExcel = null, $recalcRequired = FALSE)
47  {
48  // Create XML writer
49  $objWriter = null;
50  if ($this->getParentWriter()->getUseDiskCaching()) {
52  } else {
54  }
55 
56  // XML header
57  $objWriter->startDocument('1.0','UTF-8','yes');
58 
59  // workbook
60  $objWriter->startElement('workbook');
61  $objWriter->writeAttribute('xml:space', 'preserve');
62  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
63  $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
64 
65  // fileVersion
67 
68  // workbookPr
70 
71  // workbookProtection
72  $this->_writeWorkbookProtection($objWriter, $pPHPExcel);
73 
74  // bookViews
75  if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
76  $this->_writeBookViews($objWriter, $pPHPExcel);
77  }
78 
79  // sheets
80  $this->_writeSheets($objWriter, $pPHPExcel);
81 
82  // definedNames
83  $this->_writeDefinedNames($objWriter, $pPHPExcel);
84 
85  // calcPr
86  $this->_writeCalcPr($objWriter,$recalcRequired);
87 
88  $objWriter->endElement();
89 
90  // Return
91  return $objWriter->getData();
92  }
93 
101  {
102  $objWriter->startElement('fileVersion');
103  $objWriter->writeAttribute('appName', 'xl');
104  $objWriter->writeAttribute('lastEdited', '4');
105  $objWriter->writeAttribute('lowestEdited', '4');
106  $objWriter->writeAttribute('rupBuild', '4505');
107  $objWriter->endElement();
108  }
109 
117  {
118  $objWriter->startElement('workbookPr');
119 
121  $objWriter->writeAttribute('date1904', '1');
122  }
123 
124  $objWriter->writeAttribute('codeName', 'ThisWorkbook');
125 
126  $objWriter->endElement();
127  }
128 
136  private function _writeBookViews(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
137  {
138  // bookViews
139  $objWriter->startElement('bookViews');
140 
141  // workbookView
142  $objWriter->startElement('workbookView');
143 
144  $objWriter->writeAttribute('activeTab', $pPHPExcel->getActiveSheetIndex());
145  $objWriter->writeAttribute('autoFilterDateGrouping', '1');
146  $objWriter->writeAttribute('firstSheet', '0');
147  $objWriter->writeAttribute('minimized', '0');
148  $objWriter->writeAttribute('showHorizontalScroll', '1');
149  $objWriter->writeAttribute('showSheetTabs', '1');
150  $objWriter->writeAttribute('showVerticalScroll', '1');
151  $objWriter->writeAttribute('tabRatio', '600');
152  $objWriter->writeAttribute('visibility', 'visible');
153 
154  $objWriter->endElement();
155 
156  $objWriter->endElement();
157  }
158 
166  private function _writeWorkbookProtection(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
167  {
168  if ($pPHPExcel->getSecurity()->isSecurityEnabled()) {
169  $objWriter->startElement('workbookProtection');
170  $objWriter->writeAttribute('lockRevision', ($pPHPExcel->getSecurity()->getLockRevision() ? 'true' : 'false'));
171  $objWriter->writeAttribute('lockStructure', ($pPHPExcel->getSecurity()->getLockStructure() ? 'true' : 'false'));
172  $objWriter->writeAttribute('lockWindows', ($pPHPExcel->getSecurity()->getLockWindows() ? 'true' : 'false'));
173 
174  if ($pPHPExcel->getSecurity()->getRevisionsPassword() != '') {
175  $objWriter->writeAttribute('revisionsPassword', $pPHPExcel->getSecurity()->getRevisionsPassword());
176  }
177 
178  if ($pPHPExcel->getSecurity()->getWorkbookPassword() != '') {
179  $objWriter->writeAttribute('workbookPassword', $pPHPExcel->getSecurity()->getWorkbookPassword());
180  }
181 
182  $objWriter->endElement();
183  }
184  }
185 
193  private function _writeCalcPr(PHPExcel_Shared_XMLWriter $objWriter = null, $recalcRequired = TRUE)
194  {
195  $objWriter->startElement('calcPr');
196 
197  // Set the calcid to a higher value than Excel itself will use, otherwise Excel will always recalc
198  // If MS Excel does do a recalc, then users opening a file in MS Excel will be prompted to save on exit
199  // because the file has changed
200  $objWriter->writeAttribute('calcId', '999999');
201  $objWriter->writeAttribute('calcMode', 'auto');
202  // fullCalcOnLoad isn't needed if we've recalculating for the save
203  $objWriter->writeAttribute('calcCompleted', ($recalcRequired) ? 1 : 0);
204  $objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? 0 : 1);
205 
206  $objWriter->endElement();
207  }
208 
216  private function _writeSheets(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
217  {
218  // Write sheets
219  $objWriter->startElement('sheets');
220  $sheetCount = $pPHPExcel->getSheetCount();
221  for ($i = 0; $i < $sheetCount; ++$i) {
222  // sheet
223  $this->_writeSheet(
224  $objWriter,
225  $pPHPExcel->getSheet($i)->getTitle(),
226  ($i + 1),
227  ($i + 1 + 3),
228  $pPHPExcel->getSheet($i)->getSheetState()
229  );
230  }
231 
232  $objWriter->endElement();
233  }
234 
245  private function _writeSheet(PHPExcel_Shared_XMLWriter $objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1, $sheetState = 'visible')
246  {
247  if ($pSheetname != '') {
248  // Write sheet
249  $objWriter->startElement('sheet');
250  $objWriter->writeAttribute('name', $pSheetname);
251  $objWriter->writeAttribute('sheetId', $pSheetId);
252  if ($sheetState != 'visible' && $sheetState != '') {
253  $objWriter->writeAttribute('state', $sheetState);
254  }
255  $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
256  $objWriter->endElement();
257  } else {
258  throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
259  }
260  }
261 
269  private function _writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
270  {
271  // Write defined names
272  $objWriter->startElement('definedNames');
273 
274  // Named ranges
275  if (count($pPHPExcel->getNamedRanges()) > 0) {
276  // Named ranges
277  $this->_writeNamedRanges($objWriter, $pPHPExcel);
278  }
279 
280  // Other defined names
281  $sheetCount = $pPHPExcel->getSheetCount();
282  for ($i = 0; $i < $sheetCount; ++$i) {
283  // definedName for autoFilter
284  $this->_writeDefinedNameForAutofilter($objWriter, $pPHPExcel->getSheet($i), $i);
285 
286  // definedName for Print_Titles
287  $this->_writeDefinedNameForPrintTitles($objWriter, $pPHPExcel->getSheet($i), $i);
288 
289  // definedName for Print_Area
290  $this->_writeDefinedNameForPrintArea($objWriter, $pPHPExcel->getSheet($i), $i);
291  }
292 
293  $objWriter->endElement();
294  }
295 
303  private function _writeNamedRanges(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel)
304  {
305  // Loop named ranges
306  $namedRanges = $pPHPExcel->getNamedRanges();
307  foreach ($namedRanges as $namedRange) {
308  $this->_writeDefinedNameForNamedRange($objWriter, $namedRange);
309  }
310  }
311 
320  {
321  // definedName for named range
322  $objWriter->startElement('definedName');
323  $objWriter->writeAttribute('name', $pNamedRange->getName());
324  if ($pNamedRange->getLocalOnly()) {
325  $objWriter->writeAttribute('localSheetId', $pNamedRange->getScope()->getParent()->getIndex($pNamedRange->getScope()));
326  }
327 
328  // Create absolute coordinate and write as raw text
329  $range = PHPExcel_Cell::splitRange($pNamedRange->getRange());
330  for ($i = 0; $i < count($range); $i++) {
331  $range[$i][0] = '\'' . str_replace("'", "''", $pNamedRange->getWorksheet()->getTitle()) . '\'!' . PHPExcel_Cell::absoluteReference($range[$i][0]);
332  if (isset($range[$i][1])) {
333  $range[$i][1] = PHPExcel_Cell::absoluteReference($range[$i][1]);
334  }
335  }
336  $range = PHPExcel_Cell::buildRange($range);
337 
338  $objWriter->writeRawData($range);
339 
340  $objWriter->endElement();
341  }
342 
351  private function _writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
352  {
353  // definedName for autoFilter
354  $autoFilterRange = $pSheet->getAutoFilter()->getRange();
355  if (!empty($autoFilterRange)) {
356  $objWriter->startElement('definedName');
357  $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
358  $objWriter->writeAttribute('localSheetId', $pSheetId);
359  $objWriter->writeAttribute('hidden', '1');
360 
361  // Create absolute coordinate and write as raw text
362  $range = PHPExcel_Cell::splitRange($autoFilterRange);
363  $range = $range[0];
364  // Strip any worksheet ref so we can make the cell ref absolute
365  if (strpos($range[0],'!') !== false) {
366  list($ws,$range[0]) = explode('!',$range[0]);
367  }
368 
369  $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
370  $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
371  $range = implode(':', $range);
372 
373  $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range);
374 
375  $objWriter->endElement();
376  }
377  }
378 
387  private function _writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
388  {
389  // definedName for PrintTitles
390  if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
391  $objWriter->startElement('definedName');
392  $objWriter->writeAttribute('name', '_xlnm.Print_Titles');
393  $objWriter->writeAttribute('localSheetId', $pSheetId);
394 
395  // Setting string
396  $settingString = '';
397 
398  // Columns to repeat
399  if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
400  $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft();
401 
402  $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
403  }
404 
405  // Rows to repeat
406  if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
407  if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
408  $settingString .= ',';
409  }
410 
411  $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop();
412 
413  $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
414  }
415 
416  $objWriter->writeRawData($settingString);
417 
418  $objWriter->endElement();
419  }
420  }
421 
430  private function _writeDefinedNameForPrintArea(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
431  {
432  // definedName for PrintArea
433  if ($pSheet->getPageSetup()->isPrintAreaSet()) {
434  $objWriter->startElement('definedName');
435  $objWriter->writeAttribute('name', '_xlnm.Print_Area');
436  $objWriter->writeAttribute('localSheetId', $pSheetId);
437 
438  // Setting string
439  $settingString = '';
440 
441  // Print area
442  $printArea = PHPExcel_Cell::splitRange($pSheet->getPageSetup()->getPrintArea());
443 
444  $chunks = array();
445  foreach ($printArea as $printAreaRect) {
446  $printAreaRect[0] = PHPExcel_Cell::absoluteReference($printAreaRect[0]);
447  $printAreaRect[1] = PHPExcel_Cell::absoluteReference($printAreaRect[1]);
448  $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect);
449  }
450 
451  $objWriter->writeRawData(implode(',', $chunks));
452 
453  $objWriter->endElement();
454  }
455  }
456 }
static splitRange($pRange='A1:A1')
Split range into coordinate strings.
Definition: Cell.php:660
_writeSheets(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel $pPHPExcel=null)
Write sheets.
Definition: Workbook.php:216
_writeCalcPr(PHPExcel_Shared_XMLWriter $objWriter=null, $recalcRequired=TRUE)
Write calcPr.
Definition: Workbook.php:193
_writeFileVersion(PHPExcel_Shared_XMLWriter $objWriter=null)
Write file version.
Definition: Workbook.php:100
_writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel_Worksheet $pSheet=null, $pSheetId=0)
Write Defined Name for autoFilter.
Definition: Workbook.php:351
_writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel_Worksheet $pSheet=null, $pSheetId=0)
Write Defined Name for PrintTitles.
Definition: Workbook.php:387
_writeSheet(PHPExcel_Shared_XMLWriter $objWriter=null, $pSheetname='', $pSheetId=1, $pRelId=1, $sheetState='visible')
Write sheet.
Definition: Workbook.php:245
getParentWriter()
Get parent IWriter object.
Definition: WriterPart.php:61
_writeWorkbookPr(PHPExcel_Shared_XMLWriter $objWriter=null)
Write WorkbookPr.
Definition: Workbook.php:116
$objWriter
getNamedRanges()
Get named ranges.
Definition: PHPExcel.php:768
getName()
Get name.
Definition: NamedRange.php:104
const CALENDAR_MAC_1904
Definition: Date.php:41
_writeWorkbookProtection(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel $pPHPExcel=null)
Write WorkbookProtection.
Definition: Workbook.php:166
_writeNamedRanges(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel $pPHPExcel)
Write named ranges.
Definition: Workbook.php:303
getScope()
Get scope.
Definition: NamedRange.php:206
_writeDefinedNameForNamedRange(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel_NamedRange $pNamedRange)
Write Defined Name for named range.
Definition: Workbook.php:319
static getExcelCalendar()
Return the Excel calendar (Windows 1900 or Mac 1904)
Definition: Date.php:106
getLocalOnly()
Get localOnly.
Definition: NamedRange.php:185
_writeBookViews(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel $pPHPExcel=null)
Write BookViews.
Definition: Workbook.php:136
_writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel $pPHPExcel=null)
Write Defined Names.
Definition: Workbook.php:269
const STORAGE_MEMORY
Temporary storage method.
Definition: XMLWriter.php:46
getRange()
Get range.
Definition: NamedRange.php:163
writeWorkbook(PHPExcel $pPHPExcel=null, $recalcRequired=FALSE)
Write workbook to XML format.
Definition: Workbook.php:46
_writeDefinedNameForPrintArea(PHPExcel_Shared_XMLWriter $objWriter=null, PHPExcel_Worksheet $pSheet=null, $pSheetId=0)
Write Defined Name for PrintTitles.
Definition: Workbook.php:430
getWorksheet()
Get worksheet.
Definition: NamedRange.php:141