PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/v1.5.0/Classes/PHPExcel/Writer/Serialized.php

#
PHP | 186 lines | 72 code | 26 blank | 88 comment | 13 complexity | 4c3167cec8083ff736c15409b1961968 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel */
  28. require_once 'PHPExcel.php';
  29. /** PHPExcel_HashTable */
  30. require_once 'PHPExcel/HashTable.php';
  31. /** PHPExcel_IComparable */
  32. require_once 'PHPExcel/IComparable.php';
  33. /** PHPExcel_Worksheet */
  34. require_once 'PHPExcel/Worksheet.php';
  35. /** PHPExcel_Cell */
  36. require_once 'PHPExcel/Cell.php';
  37. /** PHPExcel_IWriter */
  38. require_once 'PHPExcel/Writer/IWriter.php';
  39. /**
  40. * PHPExcel_Writer_Serialized
  41. *
  42. * @category PHPExcel
  43. * @package PHPExcel_Writer
  44. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  45. */
  46. class PHPExcel_Writer_Serialized implements PHPExcel_Writer_IWriter
  47. {
  48. /**
  49. * Private PHPExcel
  50. *
  51. * @var PHPExcel
  52. */
  53. private $_spreadSheet;
  54. /**
  55. * Create a new PHPExcel_Writer_Serialized
  56. *
  57. * @param PHPExcel $pPHPExcel
  58. */
  59. public function __construct(PHPExcel $pPHPExcel = null)
  60. {
  61. // Assign PHPExcel
  62. $this->setPHPExcel($pPHPExcel);
  63. }
  64. /**
  65. * Save PHPExcel to file
  66. *
  67. * @param string $pFileName
  68. * @throws Exception
  69. */
  70. public function save($pFilename = null)
  71. {
  72. if (!is_null($this->_spreadSheet)) {
  73. // Create new ZIP file and open it for writing
  74. $objZip = new ZipArchive();
  75. // Try opening the ZIP file
  76. if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
  77. throw new Exception("Could not open " . $pFilename . " for writing.");
  78. }
  79. // Add media
  80. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); $i++) {
  81. for ($j = 0; $j < $this->_spreadSheet->getSheet($i)->getDrawingCollection()->count(); $j++) {
  82. if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
  83. $imgTemp = $this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j);
  84. $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath()));
  85. }
  86. }
  87. }
  88. // Add phpexcel.xml to the document, which represents a PHP serialized PHPExcel object
  89. $objZip->addFromString('phpexcel.xml', $this->_writeSerialized($this->_spreadSheet, $pFilename));
  90. // Close file
  91. if ($objZip->close() === false) {
  92. throw new Exception("Could not close zip file $pFilename.");
  93. }
  94. } else {
  95. throw new Exception("PHPExcel object unassigned.");
  96. }
  97. }
  98. /**
  99. * Get PHPExcel object
  100. *
  101. * @return PHPExcel
  102. * @throws Exception
  103. */
  104. public function getPHPExcel() {
  105. if (!is_null($this->_spreadSheet)) {
  106. return $this->_spreadSheet;
  107. } else {
  108. throw new Exception("No PHPExcel assigned.");
  109. }
  110. }
  111. /**
  112. * Get PHPExcel object
  113. *
  114. * @param PHPExcel $pPHPExcel PHPExcel object
  115. * @throws Exception
  116. */
  117. public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  118. $this->_spreadSheet = $pPHPExcel;
  119. }
  120. /**
  121. * Serialize PHPExcel object to XML
  122. *
  123. * @param PHPExcel $pPHPExcel
  124. * @param string $pFilename
  125. * @return string XML Output
  126. * @throws Exception
  127. */
  128. private function _writeSerialized(PHPExcel $pPHPExcel = null, $pFilename = '')
  129. {
  130. // Clone $pPHPExcel
  131. $pPHPExcel = clone $pPHPExcel;
  132. // Update media links
  133. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  134. for ($j = 0; $j < $pPHPExcel->getSheet($i)->getDrawingCollection()->count(); $j++) {
  135. if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
  136. $imgTemp =& $pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  137. $imgTemp->setPath('zip://' . $pFilename . '#media/' . $imgTemp->getFilename(), false);
  138. }
  139. }
  140. }
  141. // Create XML writer
  142. $objWriter = new xmlWriter();
  143. $objWriter->openMemory();
  144. $objWriter->setIndent(true);
  145. // XML header
  146. $objWriter->startDocument('1.0','UTF-8','yes');
  147. // PHPExcel
  148. $objWriter->startElement('PHPExcel');
  149. $objWriter->writeAttribute('version', '##VERSION##');
  150. // Comment
  151. $objWriter->writeComment('This file has been generated using PHPExcel v##VERSION## (http://www.codeplex.com/PHPExcel). It contains a base64 encoded serialized version of the PHPExcel internal object.');
  152. // Data
  153. $objWriter->startElement('data');
  154. $objWriter->writeCData( base64_encode(serialize($pPHPExcel)) );
  155. $objWriter->endElement();
  156. $objWriter->endElement();
  157. // Return
  158. return $objWriter->outputMemory(true);
  159. }
  160. }