PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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