PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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