PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.4.0/Classes/PHPExcel/Writer/Excel2007.php

#
PHP | 404 lines | 143 code | 65 blank | 196 comment | 16 complexity | 01411b466f2a0dd46371979dd23f1004 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. /** PHPExcel_Writer_Excel2007_WriterPart */
  40. require_once 'PHPExcel/Writer/Excel2007/WriterPart.php';
  41. /** PHPExcel_Writer_Excel2007_StringTable */
  42. require_once 'PHPExcel/Writer/Excel2007/StringTable.php';
  43. /** PHPExcel_Writer_Excel2007_ContentTypes */
  44. require_once 'PHPExcel/Writer/Excel2007/ContentTypes.php';
  45. /** PHPExcel_Writer_Excel2007_DocProps */
  46. require_once 'PHPExcel/Writer/Excel2007/DocProps.php';
  47. /** PHPExcel_Writer_Excel2007_Rels */
  48. require_once 'PHPExcel/Writer/Excel2007/Rels.php';
  49. /** PHPExcel_Writer_Excel2007_Theme */
  50. require_once 'PHPExcel/Writer/Excel2007/Theme.php';
  51. /** PHPExcel_Writer_Excel2007_Style */
  52. require_once 'PHPExcel/Writer/Excel2007/Style.php';
  53. /** PHPExcel_Writer_Excel2007_Workbook */
  54. require_once 'PHPExcel/Writer/Excel2007/Workbook.php';
  55. /** PHPExcel_Writer_Excel2007_Worksheet */
  56. require_once 'PHPExcel/Writer/Excel2007/Worksheet.php';
  57. /** PHPExcel_Writer_Excel2007_Drawing */
  58. require_once 'PHPExcel/Writer/Excel2007/Drawing.php';
  59. /**
  60. * PHPExcel_Writer_Excel2007
  61. *
  62. * @category PHPExcel
  63. * @package PHPExcel_Writer
  64. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  65. */
  66. class PHPExcel_Writer_Excel2007 implements PHPExcel_Writer_IWriter
  67. {
  68. /**
  69. * Private writer parts
  70. *
  71. * @var PHPExcel_Writer_Excel2007_WriterPart[]
  72. */
  73. private $_writerParts;
  74. /**
  75. * Private PHPExcel
  76. *
  77. * @var PHPExcel
  78. */
  79. private $_spreadSheet;
  80. /**
  81. * Private string table
  82. *
  83. * @var string[]
  84. */
  85. private $_stringTable;
  86. /**
  87. * Private unique PHPExcel_Style HashTable
  88. *
  89. * @var PHPExcel_HashTable
  90. */
  91. private $_stylesHashTable;
  92. /**
  93. * Private unique PHPExcel_Style_Conditional HashTable
  94. *
  95. * @var PHPExcel_HashTable
  96. */
  97. private $_stylesConditionalHashTable;
  98. /**
  99. * Private unique PHPExcel_Style_Fill HashTable
  100. *
  101. * @var PHPExcel_HashTable
  102. */
  103. private $_fillHashTable;
  104. /**
  105. * Private unique PHPExcel_Style_Font HashTable
  106. *
  107. * @var PHPExcel_HashTable
  108. */
  109. private $_fontHashTable;
  110. /**
  111. * Private unique PHPExcel_Style_Borders HashTable
  112. *
  113. * @var PHPExcel_HashTable
  114. */
  115. private $_bordersHashTable ;
  116. /**
  117. * Private unique PHPExcel_Style_NumberFormat HashTable
  118. *
  119. * @var PHPExcel_HashTable
  120. */
  121. private $_numFmtHashTable;
  122. /**
  123. * Private unique PHPExcel_Worksheet_Drawing HashTable
  124. *
  125. * @var PHPExcel_HashTable
  126. */
  127. private $_drawingHashTable;
  128. /**
  129. * Create a new PHPExcel_Writer_Excel2007
  130. *
  131. * @param PHPExcel $pPHPExcel
  132. */
  133. public function __construct(PHPExcel $pPHPExcel = null)
  134. {
  135. // Assign PHPExcel
  136. $this->setPHPExcel($pPHPExcel);
  137. // Initialise writer parts
  138. $this->_writerParts['stringtable'] = new PHPExcel_Writer_Excel2007_StringTable();
  139. $this->_writerParts['contenttypes'] = new PHPExcel_Writer_Excel2007_ContentTypes();
  140. $this->_writerParts['docprops'] = new PHPExcel_Writer_Excel2007_DocProps();
  141. $this->_writerParts['rels'] = new PHPExcel_Writer_Excel2007_Rels();
  142. $this->_writerParts['theme'] = new PHPExcel_Writer_Excel2007_Theme();
  143. $this->_writerParts['style'] = new PHPExcel_Writer_Excel2007_Style();
  144. $this->_writerParts['workbook'] = new PHPExcel_Writer_Excel2007_Workbook();
  145. $this->_writerParts['worksheet'] = new PHPExcel_Writer_Excel2007_Worksheet();
  146. $this->_writerParts['drawing'] = new PHPExcel_Writer_Excel2007_Drawing();
  147. // Assign parent IWriter
  148. foreach ($this->_writerParts as $writer) {
  149. $writer->setParentWriter($this);
  150. }
  151. // Set HashTable variables
  152. $this->_stringTable = array();
  153. $this->_stylesHashTable = new PHPExcel_HashTable();
  154. $this->_stylesConditionalHashTable = new PHPExcel_HashTable();
  155. $this->_fillHashTable = new PHPExcel_HashTable();
  156. $this->_fontHashTable = new PHPExcel_HashTable();
  157. $this->_bordersHashTable = new PHPExcel_HashTable();
  158. $this->_numFmtHashTable = new PHPExcel_HashTable();
  159. $this->_drawingHashTable = new PHPExcel_HashTable();
  160. // Other initializations
  161. $this->_serializePHPExcel = false;
  162. }
  163. /**
  164. * Get writer part
  165. *
  166. * @param string $pPartName Writer part name
  167. * @return PHPExcel_Writer_Excel2007_WriterPart
  168. */
  169. function getWriterPart($pPartName = '') {
  170. if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  171. return $this->_writerParts[strtolower($pPartName)];
  172. } else {
  173. return null;
  174. }
  175. }
  176. /**
  177. * Save PHPExcel to file
  178. *
  179. * @param string $pFileName
  180. * @throws Exception
  181. */
  182. public function save($pFilename = null)
  183. {
  184. if (!is_null($this->_spreadSheet)) {
  185. // Create string lookup table
  186. $this->_stringTable = array();
  187. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); $i++) {
  188. $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
  189. }
  190. // Create styles dictionaries
  191. $this->_stylesHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_spreadSheet) );
  192. $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
  193. $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
  194. $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
  195. $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
  196. $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
  197. // Create drawing dictionary
  198. $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
  199. // Create new ZIP file and open it for writing
  200. $objZip = new ZipArchive();
  201. // Try opening the ZIP file
  202. if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
  203. throw new Exception("Could not open " . $pFilename . " for writing!");
  204. }
  205. // Add [Content_Types].xml to ZIP file
  206. $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
  207. // Add relationships to ZIP file
  208. $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  209. $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  210. // Add document properties to ZIP file
  211. $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  212. $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  213. // Add theme to ZIP file
  214. $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  215. // Add string table to ZIP file
  216. $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  217. // Add styles to ZIP file
  218. $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  219. // Add workbook to ZIP file
  220. $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
  221. // Add worksheets
  222. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); $i++) {
  223. $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable));
  224. }
  225. // Add worksheet relationships (drawings, ...)
  226. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); $i++) {
  227. // Currently, only drawing collection should be checked for relations.
  228. // In the future, update this construction!
  229. if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
  230. // Worksheet relationships
  231. $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
  232. }
  233. // If sheet contains drawings, add the relationships
  234. if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
  235. // Drawing relationships
  236. $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
  237. // Drawings
  238. $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
  239. }
  240. }
  241. // Add media
  242. for ($i = 0; $i < $this->getDrawingHashTable()->count(); $i++) {
  243. $objZip->addFromString('xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getFilename(), file_get_contents($this->getDrawingHashTable()->getByIndex($i)->getPath()));
  244. //The line underneath does not support adding a file from a ZIP archive, the line above does!
  245. //$objZip->addFile($this->getDrawingHashTable()->getByIndex($i)->getPath(), 'xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getFilename());
  246. }
  247. // Close file
  248. $objZip->close();
  249. } else {
  250. throw new Exception("PHPExcel object unassigned.");
  251. }
  252. }
  253. /**
  254. * Get PHPExcel object
  255. *
  256. * @return PHPExcel
  257. * @throws Exception
  258. */
  259. public function getPHPExcel() {
  260. if (!is_null($this->_spreadSheet)) {
  261. return $this->_spreadSheet;
  262. } else {
  263. throw new Exception("No PHPExcel assigned.");
  264. }
  265. }
  266. /**
  267. * Get PHPExcel object
  268. *
  269. * @param PHPExcel $pPHPExcel PHPExcel object
  270. * @throws Exception
  271. */
  272. public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  273. $this->_spreadSheet = $pPHPExcel;
  274. }
  275. /**
  276. * Get string table
  277. *
  278. * @return string[]
  279. */
  280. public function getStringTable() {
  281. return $this->_stringTable;
  282. }
  283. /**
  284. * Get PHPExcel_Style HashTable
  285. *
  286. * @return PHPExcel_HashTable
  287. */
  288. public function getStylesHashTable() {
  289. return $this->_stylesHashTable;
  290. }
  291. /**
  292. * Get PHPExcel_Style_Conditional HashTable
  293. *
  294. * @return PHPExcel_HashTable
  295. */
  296. public function getStylesConditionalHashTable() {
  297. return $this->_stylesConditionalHashTable;
  298. }
  299. /**
  300. * Get PHPExcel_Style_Fill HashTable
  301. *
  302. * @return PHPExcel_HashTable
  303. */
  304. public function getFillHashTable() {
  305. return $this->_fillHashTable;
  306. }
  307. /**
  308. * Get PHPExcel_Style_Font HashTable
  309. *
  310. * @return PHPExcel_HashTable
  311. */
  312. public function getFontHashTable() {
  313. return $this->_fontHashTable;
  314. }
  315. /**
  316. * Get PHPExcel_Style_Borders HashTable
  317. *
  318. * @return PHPExcel_HashTable
  319. */
  320. public function getBordersHashTable() {
  321. return $this->_bordersHashTable;
  322. }
  323. /**
  324. * Get PHPExcel_Style_NumberFormat HashTable
  325. *
  326. * @return PHPExcel_HashTable
  327. */
  328. public function getNumFmtHashTable() {
  329. return $this->_numFmtHashTable;
  330. }
  331. /**
  332. * Get PHPExcel_Worksheet_Drawing HashTable
  333. *
  334. * @return PHPExcel_HashTable
  335. */
  336. public function getDrawingHashTable() {
  337. return $this->_drawingHashTable;
  338. }
  339. }