ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Excel2007.php
Go to the documentation of this file.
1<?php
37{
47 protected $_preCalculateFormulas = FALSE;
48
55
61 private $_writerParts = array();
62
69
75 private $_stringTable = array();
76
83
90
97
104
111
118
125
131 public function __construct(PHPExcel $pPHPExcel = null)
132 {
133 // Assign PHPExcel
134 $this->setPHPExcel($pPHPExcel);
135
136 $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
137 'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
138 'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
139 'rels' => 'PHPExcel_Writer_Excel2007_Rels',
140 'theme' => 'PHPExcel_Writer_Excel2007_Theme',
141 'style' => 'PHPExcel_Writer_Excel2007_Style',
142 'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
143 'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
144 'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
145 'comments' => 'PHPExcel_Writer_Excel2007_Comments',
146 'chart' => 'PHPExcel_Writer_Excel2007_Chart',
147 'relsvba' => 'PHPExcel_Writer_Excel2007_RelsVBA',
148 'relsribbonobjects' => 'PHPExcel_Writer_Excel2007_RelsRibbon'
149 );
150
151 // Initialise writer parts
152 // and Assign their parent IWriters
153 foreach ($writerPartsArray as $writer => $class) {
154 $this->_writerParts[$writer] = new $class($this);
155 }
156
157 $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable',
158 '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable',
159 '_styleHashTable'
160 );
161
162 // Set HashTable variables
163 foreach ($hashTablesArray as $tableName) {
164 $this->$tableName = new PHPExcel_HashTable();
165 }
166 }
167
174 public function getWriterPart($pPartName = '') {
175 if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
176 return $this->_writerParts[strtolower($pPartName)];
177 } else {
178 return null;
179 }
180 }
181
188 public function save($pFilename = null)
189 {
190 if ($this->_spreadSheet !== NULL) {
191 // garbage collect
192 $this->_spreadSheet->garbageCollect();
193
194 // If $pFilename is php://output or php://stdout, make it a temporary file...
195 $originalFilename = $pFilename;
196 if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
197 $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
198 if ($pFilename == '') {
199 $pFilename = $originalFilename;
200 }
201 }
202
203 $saveDebugLog = PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->getWriteDebugLog();
204 PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog(FALSE);
207
208 // Create string lookup table
209 $this->_stringTable = array();
210 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
211 $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
212 }
213
214 // Create styles dictionaries
215 $this->_styleHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_spreadSheet) );
216 $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
217 $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
218 $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
219 $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
220 $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
221
222 // Create drawing dictionary
223 $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
224
225 // Create new ZIP file and open it for writing
226 $zipClass = PHPExcel_Settings::getZipClass();
227 $objZip = new $zipClass();
228
229 // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
230 // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
231 $ro = new ReflectionObject($objZip);
232 $zipOverWrite = $ro->getConstant('OVERWRITE');
233 $zipCreate = $ro->getConstant('CREATE');
234
235 if (file_exists($pFilename)) {
236 unlink($pFilename);
237 }
238 // Try opening the ZIP file
239 if ($objZip->open($pFilename, $zipOverWrite) !== true) {
240 if ($objZip->open($pFilename, $zipCreate) !== true) {
241 throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
242 }
243 }
244
245 // Add [Content_Types].xml to ZIP file
246 $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts));
247
248 //if hasMacros, add the vbaProject.bin file, Certificate file(if exists)
249 if($this->_spreadSheet->hasMacros()){
250 $macrosCode=$this->_spreadSheet->getMacrosCode();
251 if(!is_null($macrosCode)){// we have the code ?
252 $objZip->addFromString('xl/vbaProject.bin', $macrosCode);//allways in 'xl', allways named vbaProject.bin
253 if($this->_spreadSheet->hasMacrosCertificate()){//signed macros ?
254 // Yes : add the certificate file and the related rels file
255 $objZip->addFromString('xl/vbaProjectSignature.bin', $this->_spreadSheet->getMacrosCertificate());
256 $objZip->addFromString('xl/_rels/vbaProject.bin.rels',
257 $this->getWriterPart('RelsVBA')->writeVBARelationships($this->_spreadSheet));
258 }
259 }
260 }
261 //a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
262 if($this->_spreadSheet->hasRibbon()){
263 $tmpRibbonTarget=$this->_spreadSheet->getRibbonXMLData('target');
264 $objZip->addFromString($tmpRibbonTarget, $this->_spreadSheet->getRibbonXMLData('data'));
265 if($this->_spreadSheet->hasRibbonBinObjects()){
266 $tmpRootPath=dirname($tmpRibbonTarget).'/';
267 $ribbonBinObjects=$this->_spreadSheet->getRibbonBinObjects('data');//the files to write
268 foreach($ribbonBinObjects as $aPath=>$aContent){
269 $objZip->addFromString($tmpRootPath.$aPath, $aContent);
270 }
271 //the rels for files
272 $objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels',
273 $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->_spreadSheet));
274 }
275 }
276
277 // Add relationships to ZIP file
278 $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
279 $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
280
281 // Add document properties to ZIP file
282 $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
283 $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
284 $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
285 if ($customPropertiesPart !== NULL) {
286 $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
287 }
288
289 // Add theme to ZIP file
290 $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
291
292 // Add string table to ZIP file
293 $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
294
295 // Add styles to ZIP file
296 $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
297
298 // Add workbook to ZIP file
299 $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas));
300
301 $chartCount = 0;
302 // Add worksheets
303 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
304 $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts));
305 if ($this->_includeCharts) {
306 $charts = $this->_spreadSheet->getSheet($i)->getChartCollection();
307 if (count($charts) > 0) {
308 foreach($charts as $chart) {
309 $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart));
310 $chartCount++;
311 }
312 }
313 }
314 }
315
316 $chartRef1 = $chartRef2 = 0;
317 // Add worksheet relationships (drawings, ...)
318 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
319
320 // Add relationships
321 $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts));
322
323 $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection();
324 $drawingCount = count($drawings);
325 if ($this->_includeCharts) {
326 $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount();
327 }
328
329 // Add drawing and image relationship parts
330 if (($drawingCount > 0) || ($chartCount > 0)) {
331 // Drawing relationships
332 $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts));
333
334 // Drawings
335 $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts));
336 }
337
338 // Add comment relationship parts
339 if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
340 // VML Comments
341 $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
342
343 // Comments
344 $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
345 }
346
347 // Add header/footer relationship parts
348 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
349 // VML Drawings
350 $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
351
352 // VML Drawing relationships
353 $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
354
355 // Media
356 foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
357 $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
358 }
359 }
360 }
361
362 // Add media
363 for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
364 if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
365 $imageContents = null;
366 $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
367 if (strpos($imagePath, 'zip://') !== false) {
368 $imagePath = substr($imagePath, 6);
369 $imagePathSplitted = explode('#', $imagePath);
370
371 $imageZip = new ZipArchive();
372 $imageZip->open($imagePathSplitted[0]);
373 $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
374 $imageZip->close();
375 unset($imageZip);
376 } else {
377 $imageContents = file_get_contents($imagePath);
378 }
379
380 $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
381 } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
382 ob_start();
383 call_user_func(
384 $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
385 $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
386 );
387 $imageContents = ob_get_contents();
388 ob_end_clean();
389
390 $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
391 }
392 }
393
395 PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
396
397 // Close file
398 if ($objZip->close() === false) {
399 throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
400 }
401
402 // If a temporary file was used, copy it to the correct file stream
403 if ($originalFilename != $pFilename) {
404 if (copy($pFilename, $originalFilename) === false) {
405 throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
406 }
407 @unlink($pFilename);
408 }
409 } else {
410 throw new PHPExcel_Writer_Exception("PHPExcel object unassigned.");
411 }
412 }
413
420 public function getPHPExcel() {
421 if ($this->_spreadSheet !== null) {
422 return $this->_spreadSheet;
423 } else {
424 throw new PHPExcel_Writer_Exception("No PHPExcel assigned.");
425 }
426 }
427
435 public function setPHPExcel(PHPExcel $pPHPExcel = null) {
436 $this->_spreadSheet = $pPHPExcel;
437 return $this;
438 }
439
445 public function getStringTable() {
446 return $this->_stringTable;
447 }
448
454 public function getStyleHashTable() {
456 }
457
465 }
466
472 public function getFillHashTable() {
474 }
475
481 public function getFontHashTable() {
483 }
484
490 public function getBordersHashTable() {
492 }
493
499 public function getNumFmtHashTable() {
501 }
502
508 public function getDrawingHashTable() {
510 }
511
517 public function getOffice2003Compatibility() {
519 }
520
527 public function setOffice2003Compatibility($pValue = false) {
528 $this->_office2003compatibility = $pValue;
529 return $this;
530 }
531
532}
An exception for terminatinating execution or to throw for unit testing.
static setReturnDateType($returnDateType)
Definition: Functions.php:155
static getInstance(PHPExcel $workbook=NULL)
Get an instance of this class.
static getZipClass()
Return the name of the Zip handler Class that PHPExcel is configured to use (PCLZip or ZipArchive) or...
Definition: Settings.php:141
static sys_get_temp_dir()
Get the systems temporary directory.
Definition: File.php:135
getNumFmtHashTable()
Get PHPExcel_Style_NumberFormat HashTable.
Definition: Excel2007.php:499
getStyleHashTable()
Get PHPExcel_Style HashTable.
Definition: Excel2007.php:454
getWriterPart($pPartName='')
Get writer part.
Definition: Excel2007.php:174
getFillHashTable()
Get PHPExcel_Style_Fill HashTable.
Definition: Excel2007.php:472
getOffice2003Compatibility()
Get Office2003 compatibility.
Definition: Excel2007.php:517
getPHPExcel()
Get PHPExcel object.
Definition: Excel2007.php:420
getStylesConditionalHashTable()
Get PHPExcel_Style_Conditional HashTable.
Definition: Excel2007.php:463
__construct(PHPExcel $pPHPExcel=null)
Create a new PHPExcel_Writer_Excel2007.
Definition: Excel2007.php:131
getBordersHashTable()
Get PHPExcel_Style_Borders HashTable.
Definition: Excel2007.php:490
getFontHashTable()
Get PHPExcel_Style_Font HashTable.
Definition: Excel2007.php:481
getStringTable()
Get string table.
Definition: Excel2007.php:445
save($pFilename=null)
Save PHPExcel to file.
Definition: Excel2007.php:188
setPHPExcel(PHPExcel $pPHPExcel=null)
Set PHPExcel object.
Definition: Excel2007.php:435
getDrawingHashTable()
Get PHPExcel_Worksheet_BaseDrawing HashTable.
Definition: Excel2007.php:508
setOffice2003Compatibility($pValue=false)
Set Office2003 compatibility.
Definition: Excel2007.php:527
$i
Definition: disco.tpl.php:19