ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Excel2007.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/HashTable.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
51 
53 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
54 
56 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
57 
59 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
60 
62 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/StringTable.php';
63 
65 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/ContentTypes.php';
66 
68 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/DocProps.php';
69 
71 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Rels.php';
72 
74 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Theme.php';
75 
77 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Style.php';
78 
80 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Workbook.php';
81 
83 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Worksheet.php';
84 
86 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Drawing.php';
87 
89 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/Comments.php';
90 
91 
100 {
106  private $_preCalculateFormulas = true;
107 
113  private $_office2003compatibility = false;
114 
120  private $_writerParts;
121 
127  private $_spreadSheet;
128 
134  private $_stringTable;
135 
142 
149 
156 
163 
170 
177 
183  private $_useDiskCaching = false;
184 
191 
197  public function __construct(PHPExcel $pPHPExcel = null)
198  {
199  // Assign PHPExcel
200  $this->setPHPExcel($pPHPExcel);
201 
202  // Set up disk caching location
203  $this->_diskCachingDirectory = './';
204 
205  // Initialise writer parts
206  $this->_writerParts['stringtable'] = new PHPExcel_Writer_Excel2007_StringTable();
207  $this->_writerParts['contenttypes'] = new PHPExcel_Writer_Excel2007_ContentTypes();
208  $this->_writerParts['docprops'] = new PHPExcel_Writer_Excel2007_DocProps();
209  $this->_writerParts['rels'] = new PHPExcel_Writer_Excel2007_Rels();
210  $this->_writerParts['theme'] = new PHPExcel_Writer_Excel2007_Theme();
211  $this->_writerParts['style'] = new PHPExcel_Writer_Excel2007_Style();
212  $this->_writerParts['workbook'] = new PHPExcel_Writer_Excel2007_Workbook();
213  $this->_writerParts['worksheet'] = new PHPExcel_Writer_Excel2007_Worksheet();
214  $this->_writerParts['drawing'] = new PHPExcel_Writer_Excel2007_Drawing();
215  $this->_writerParts['comments'] = new PHPExcel_Writer_Excel2007_Comments();
216 
217  // Assign parent IWriter
218  foreach ($this->_writerParts as $writer) {
219  $writer->setParentWriter($this);
220  }
221 
222  // Set HashTable variables
223  $this->_stringTable = array();
224  $this->_stylesConditionalHashTable = new PHPExcel_HashTable();
225  $this->_fillHashTable = new PHPExcel_HashTable();
226  $this->_fontHashTable = new PHPExcel_HashTable();
227  $this->_bordersHashTable = new PHPExcel_HashTable();
228  $this->_numFmtHashTable = new PHPExcel_HashTable();
229  $this->_drawingHashTable = new PHPExcel_HashTable();
230  }
231 
238  function getWriterPart($pPartName = '') {
239  if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
240  return $this->_writerParts[strtolower($pPartName)];
241  } else {
242  return null;
243  }
244  }
245 
252  public function save($pFilename = null)
253  {
254  if (!is_null($this->_spreadSheet)) {
255  // garbage collect
256  $this->_spreadSheet->garbageCollect();
257 
258  // If $pFilename is php://output or php://stdout, make it a temporary file...
259  $originalFilename = $pFilename;
260  if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
261  $pFilename = @tempnam('./', 'phpxltmp');
262  if ($pFilename == '') {
263  $pFilename = $originalFilename;
264  }
265  }
266 
269 
270  // Create string lookup table
271  $this->_stringTable = array();
272  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
273  $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
274  }
275 
276  // Create styles dictionaries
277  $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
278  $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
279  $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
280  $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
281  $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
282 
283  // Create drawing dictionary
284  $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
285 
286  // Create new ZIP file and open it for writing
287  $objZip = new ZipArchive();
288 
289  // Try opening the ZIP file
290  if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
291  if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
292  throw new Exception("Could not open " . $pFilename . " for writing.");
293  }
294  }
295 
296  // Add [Content_Types].xml to ZIP file
297  $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
298 
299  // Add relationships to ZIP file
300  $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
301  $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
302 
303  // Add document properties to ZIP file
304  $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
305  $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
306 
307  // Add theme to ZIP file
308  $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
309 
310  // Add string table to ZIP file
311  $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
312 
313  // Add styles to ZIP file
314  $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
315 
316  // Add workbook to ZIP file
317  $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
318 
319  // Add worksheets
320  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
321  $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable));
322  }
323 
324  // Add worksheet relationships (drawings, ...)
325  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
326 
327  // Add relationships
328  $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
329 
330  // Add drawing relationship parts
331  if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
332  // Drawing relationships
333  $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
334 
335  // Drawings
336  $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
337  }
338 
339  // Add comment relationship parts
340  if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
341  // VML Comments
342  $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
343 
344  // Comments
345  $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
346  }
347 
348  // Add header/footer relationship parts
349  if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
350  // VML Drawings
351  $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
352 
353  // VML Drawing relationships
354  $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
355 
356  // Media
357  foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
358  $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
359  }
360  }
361  }
362 
363  // Add media
364  for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
365  if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
366  $imageContents = null;
367  $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
368 
369  if (strpos($imagePath, 'zip://') !== false) {
370  $imagePath = substr($imagePath, 6);
371  $imagePathSplitted = explode('#', $imagePath);
372 
373  $imageZip = new ZipArchive();
374  $imageZip->open($imagePathSplitted[0]);
375  $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
376  $imageZip->close();
377  unset($imageZip);
378  } else {
379  $imageContents = file_get_contents($imagePath);
380  }
381 
382  $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
383  } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
384  ob_start();
385  call_user_func(
386  $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
387  $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
388  );
389  $imageContents = ob_get_contents();
390  ob_end_clean();
391 
392  $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
393  }
394  }
395 
397 
398  // Close file
399  if ($objZip->close() === false) {
400  throw new Exception("Could not close zip file $pFilename.");
401  }
402 
403  // If a temporary file was used, copy it to the correct file stream
404  if ($originalFilename != $pFilename) {
405  if (copy($pFilename, $originalFilename) === false) {
406  throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
407  }
408  @unlink($pFilename);
409  }
410  } else {
411  throw new Exception("PHPExcel object unassigned.");
412  }
413  }
414 
421  public function getPHPExcel() {
422  if (!is_null($this->_spreadSheet)) {
423  return $this->_spreadSheet;
424  } else {
425  throw new Exception("No PHPExcel assigned.");
426  }
427  }
428 
436  public function setPHPExcel(PHPExcel $pPHPExcel = null) {
437  $this->_spreadSheet = $pPHPExcel;
438  return $this;
439  }
440 
446  public function getStringTable() {
447  return $this->_stringTable;
448  }
449 
455  public function getStylesConditionalHashTable() {
457  }
458 
464  public function getFillHashTable() {
465  return $this->_fillHashTable;
466  }
467 
473  public function getFontHashTable() {
474  return $this->_fontHashTable;
475  }
476 
482  public function getBordersHashTable() {
484  }
485 
491  public function getNumFmtHashTable() {
493  }
494 
500  public function getDrawingHashTable() {
502  }
503 
509  public function getPreCalculateFormulas() {
511  }
512 
518  public function setPreCalculateFormulas($pValue = true) {
519  $this->_preCalculateFormulas = $pValue;
520  }
521 
527  public function getOffice2003Compatibility() {
529  }
530 
537  public function setOffice2003Compatibility($pValue = false) {
538  $this->_office2003compatibility = $pValue;
539  return $this;
540  }
541 
547  public function getUseDiskCaching() {
548  return $this->_useDiskCaching;
549  }
550 
559  public function setUseDiskCaching($pValue = false, $pDirectory = null) {
560  $this->_useDiskCaching = $pValue;
561 
562  if (!is_null($pDirectory)) {
563  if (is_dir($pDirectory)) {
564  $this->_diskCachingDirectory = $pDirectory;
565  } else {
566  throw new Exception("Directory does not exist: $pDirectory");
567  }
568  }
569  return $this;
570  }
571 
577  public function getDiskCachingDirectory() {
579  }
580 }