/PHPExcel/Writer/Excel2007.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 583 lines · 249 code · 75 blank · 259 comment · 42 complexity · 6bd65aa59a63a1daceb11d166b3bacb9 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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_Excel2007
  23. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007 implements PHPExcel_Writer_IWriter
  35. {
  36. /**
  37. * Write charts that are defined in the workbook?
  38. * Identifies whether the Writer should write definitions for any charts that exist in the PHPExcel object;
  39. *
  40. * @var boolean
  41. */
  42. private $_includeCharts = false;
  43. /**
  44. * Pre-calculate formulas
  45. *
  46. * @var boolean
  47. */
  48. private $_preCalculateFormulas = true;
  49. /**
  50. * Office2003 compatibility
  51. *
  52. * @var boolean
  53. */
  54. private $_office2003compatibility = false;
  55. /**
  56. * Private writer parts
  57. *
  58. * @var PHPExcel_Writer_Excel2007_WriterPart[]
  59. */
  60. private $_writerParts = array();
  61. /**
  62. * Private PHPExcel
  63. *
  64. * @var PHPExcel
  65. */
  66. private $_spreadSheet;
  67. /**
  68. * Private string table
  69. *
  70. * @var string[]
  71. */
  72. private $_stringTable = array();
  73. /**
  74. * Private unique PHPExcel_Style_Conditional HashTable
  75. *
  76. * @var PHPExcel_HashTable
  77. */
  78. private $_stylesConditionalHashTable;
  79. /**
  80. * Private unique PHPExcel_Style_Fill HashTable
  81. *
  82. * @var PHPExcel_HashTable
  83. */
  84. private $_fillHashTable;
  85. /**
  86. * Private unique PHPExcel_Style_Font HashTable
  87. *
  88. * @var PHPExcel_HashTable
  89. */
  90. private $_fontHashTable;
  91. /**
  92. * Private unique PHPExcel_Style_Borders HashTable
  93. *
  94. * @var PHPExcel_HashTable
  95. */
  96. private $_bordersHashTable ;
  97. /**
  98. * Private unique PHPExcel_Style_NumberFormat HashTable
  99. *
  100. * @var PHPExcel_HashTable
  101. */
  102. private $_numFmtHashTable;
  103. /**
  104. * Private unique PHPExcel_Worksheet_BaseDrawing HashTable
  105. *
  106. * @var PHPExcel_HashTable
  107. */
  108. private $_drawingHashTable;
  109. /**
  110. * Use disk caching where possible?
  111. *
  112. * @var boolean
  113. */
  114. private $_useDiskCaching = false;
  115. /**
  116. * Disk caching directory
  117. *
  118. * @var string
  119. */
  120. private $_diskCachingDirectory = './';
  121. /**
  122. * Create a new PHPExcel_Writer_Excel2007
  123. *
  124. * @param PHPExcel $pPHPExcel
  125. */
  126. public function __construct(PHPExcel $pPHPExcel = null)
  127. {
  128. // Assign PHPExcel
  129. $this->setPHPExcel($pPHPExcel);
  130. $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
  131. 'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
  132. 'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
  133. 'rels' => 'PHPExcel_Writer_Excel2007_Rels',
  134. 'theme' => 'PHPExcel_Writer_Excel2007_Theme',
  135. 'style' => 'PHPExcel_Writer_Excel2007_Style',
  136. 'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
  137. 'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
  138. 'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
  139. 'comments' => 'PHPExcel_Writer_Excel2007_Comments',
  140. 'chart' => 'PHPExcel_Writer_Excel2007_Chart',
  141. );
  142. // Initialise writer parts
  143. // and Assign their parent IWriters
  144. foreach ($writerPartsArray as $writer => $class) {
  145. $this->_writerParts[$writer] = new $class($this);
  146. }
  147. $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable',
  148. '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable'
  149. );
  150. // Set HashTable variables
  151. foreach ($hashTablesArray as $tableName) {
  152. $this->$tableName = new PHPExcel_HashTable();
  153. }
  154. }
  155. /**
  156. * Get writer part
  157. *
  158. * @param string $pPartName Writer part name
  159. * @return PHPExcel_Writer_Excel2007_WriterPart
  160. */
  161. public function getWriterPart($pPartName = '') {
  162. if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  163. return $this->_writerParts[strtolower($pPartName)];
  164. } else {
  165. return null;
  166. }
  167. }
  168. /**
  169. * Save PHPExcel to file
  170. *
  171. * @param string $pFilename
  172. * @throws Exception
  173. */
  174. public function save($pFilename = null)
  175. {
  176. if ($this->_spreadSheet !== NULL) {
  177. // garbage collect
  178. $this->_spreadSheet->garbageCollect();
  179. // If $pFilename is php://output or php://stdout, make it a temporary file...
  180. $originalFilename = $pFilename;
  181. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  182. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  183. if ($pFilename == '') {
  184. $pFilename = $originalFilename;
  185. }
  186. }
  187. $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
  188. PHPExcel_Calculation::getInstance()->writeDebugLog = false;
  189. $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  190. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  191. // Create string lookup table
  192. $this->_stringTable = array();
  193. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  194. $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
  195. }
  196. // Create styles dictionaries
  197. $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
  198. $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
  199. $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
  200. $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
  201. $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
  202. // Create drawing dictionary
  203. $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
  204. // Create new ZIP file and open it for writing
  205. $zipClass = PHPExcel_Settings::getZipClass();
  206. $objZip = new $zipClass();
  207. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  208. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  209. $ro = new ReflectionObject($objZip);
  210. $zipOverWrite = $ro->getConstant('OVERWRITE');
  211. $zipCreate = $ro->getConstant('CREATE');
  212. if (file_exists($pFilename)) {
  213. unlink($pFilename);
  214. }
  215. // Try opening the ZIP file
  216. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  217. if ($objZip->open($pFilename, $zipCreate) !== true) {
  218. throw new Exception("Could not open " . $pFilename . " for writing.");
  219. }
  220. }
  221. // Add [Content_Types].xml to ZIP file
  222. $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts));
  223. // Add relationships to ZIP file
  224. $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  225. $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  226. // Add document properties to ZIP file
  227. $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  228. $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  229. $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
  230. if ($customPropertiesPart !== NULL) {
  231. $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
  232. }
  233. // Add theme to ZIP file
  234. $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  235. // Add string table to ZIP file
  236. $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  237. // Add styles to ZIP file
  238. $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  239. // Add workbook to ZIP file
  240. $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas));
  241. $chartCount = 0;
  242. // Add worksheets
  243. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  244. $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts));
  245. if ($this->_includeCharts) {
  246. $charts = $this->_spreadSheet->getSheet($i)->getChartCollection();
  247. if (count($charts) > 0) {
  248. foreach($charts as $chart) {
  249. $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart));
  250. $chartCount++;
  251. }
  252. }
  253. }
  254. }
  255. $chartRef1 = $chartRef2 = 0;
  256. // Add worksheet relationships (drawings, ...)
  257. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  258. // Add relationships
  259. $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts));
  260. $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection();
  261. $drawingCount = count($drawings);
  262. if ($this->_includeCharts) {
  263. $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount();
  264. }
  265. // Add drawing and image relationship parts
  266. if (($drawingCount > 0) || ($chartCount > 0)) {
  267. // Drawing relationships
  268. $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts));
  269. // Drawings
  270. $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts));
  271. }
  272. // Add comment relationship parts
  273. if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
  274. // VML Comments
  275. $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
  276. // Comments
  277. $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
  278. }
  279. // Add header/footer relationship parts
  280. if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
  281. // VML Drawings
  282. $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
  283. // VML Drawing relationships
  284. $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
  285. // Media
  286. foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
  287. $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
  288. }
  289. }
  290. }
  291. // Add media
  292. for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
  293. if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  294. $imageContents = null;
  295. $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
  296. if (strpos($imagePath, 'zip://') !== false) {
  297. $imagePath = substr($imagePath, 6);
  298. $imagePathSplitted = explode('#', $imagePath);
  299. $imageZip = new ZipArchive();
  300. $imageZip->open($imagePathSplitted[0]);
  301. $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
  302. $imageZip->close();
  303. unset($imageZip);
  304. } else {
  305. $imageContents = file_get_contents($imagePath);
  306. }
  307. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  308. } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  309. ob_start();
  310. call_user_func(
  311. $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  312. $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  313. );
  314. $imageContents = ob_get_contents();
  315. ob_end_clean();
  316. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  317. }
  318. }
  319. PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  320. PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
  321. // Close file
  322. if ($objZip->close() === false) {
  323. throw new Exception("Could not close zip file $pFilename.");
  324. }
  325. // If a temporary file was used, copy it to the correct file stream
  326. if ($originalFilename != $pFilename) {
  327. if (copy($pFilename, $originalFilename) === false) {
  328. throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  329. }
  330. @unlink($pFilename);
  331. }
  332. } else {
  333. throw new Exception("PHPExcel object unassigned.");
  334. }
  335. }
  336. /**
  337. * Get PHPExcel object
  338. *
  339. * @return PHPExcel
  340. * @throws Exception
  341. */
  342. public function getPHPExcel() {
  343. if ($this->_spreadSheet !== null) {
  344. return $this->_spreadSheet;
  345. } else {
  346. throw new Exception("No PHPExcel assigned.");
  347. }
  348. }
  349. /**
  350. * Set PHPExcel object
  351. *
  352. * @param PHPExcel $pPHPExcel PHPExcel object
  353. * @throws Exception
  354. * @return PHPExcel_Writer_Excel2007
  355. */
  356. public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  357. $this->_spreadSheet = $pPHPExcel;
  358. return $this;
  359. }
  360. /**
  361. * Get string table
  362. *
  363. * @return string[]
  364. */
  365. public function getStringTable() {
  366. return $this->_stringTable;
  367. }
  368. /**
  369. * Get PHPExcel_Style_Conditional HashTable
  370. *
  371. * @return PHPExcel_HashTable
  372. */
  373. public function getStylesConditionalHashTable() {
  374. return $this->_stylesConditionalHashTable;
  375. }
  376. /**
  377. * Get PHPExcel_Style_Fill HashTable
  378. *
  379. * @return PHPExcel_HashTable
  380. */
  381. public function getFillHashTable() {
  382. return $this->_fillHashTable;
  383. }
  384. /**
  385. * Get PHPExcel_Style_Font HashTable
  386. *
  387. * @return PHPExcel_HashTable
  388. */
  389. public function getFontHashTable() {
  390. return $this->_fontHashTable;
  391. }
  392. /**
  393. * Get PHPExcel_Style_Borders HashTable
  394. *
  395. * @return PHPExcel_HashTable
  396. */
  397. public function getBordersHashTable() {
  398. return $this->_bordersHashTable;
  399. }
  400. /**
  401. * Get PHPExcel_Style_NumberFormat HashTable
  402. *
  403. * @return PHPExcel_HashTable
  404. */
  405. public function getNumFmtHashTable() {
  406. return $this->_numFmtHashTable;
  407. }
  408. /**
  409. * Get PHPExcel_Worksheet_BaseDrawing HashTable
  410. *
  411. * @return PHPExcel_HashTable
  412. */
  413. public function getDrawingHashTable() {
  414. return $this->_drawingHashTable;
  415. }
  416. /**
  417. * Write charts in workbook?
  418. * If this is true, then the Writer will write definitions for any charts that exist in the PHPExcel object.
  419. * If false (the default) it will ignore any charts defined in the PHPExcel object.
  420. *
  421. * @return boolean
  422. */
  423. public function getIncludeCharts() {
  424. return $this->_includeCharts;
  425. }
  426. /**
  427. * Set write charts in workbook
  428. * Set to true, to advise the Writer to include any charts that exist in the PHPExcel object.
  429. * Set to false (the default) to ignore charts.
  430. *
  431. * @param boolean $pValue
  432. *
  433. * @return PHPExcel_Writer_Excel2007
  434. */
  435. public function setIncludeCharts($pValue = false) {
  436. $this->_includeCharts = (boolean) $pValue;
  437. return $this;
  438. }
  439. /**
  440. * Get Pre-Calculate Formulas
  441. *
  442. * @return boolean
  443. */
  444. public function getPreCalculateFormulas() {
  445. return $this->_preCalculateFormulas;
  446. }
  447. /**
  448. * Set Pre-Calculate Formulas
  449. *
  450. * @param boolean $pValue Pre-Calculate Formulas?
  451. */
  452. public function setPreCalculateFormulas($pValue = true) {
  453. $this->_preCalculateFormulas = $pValue;
  454. }
  455. /**
  456. * Get Office2003 compatibility
  457. *
  458. * @return boolean
  459. */
  460. public function getOffice2003Compatibility() {
  461. return $this->_office2003compatibility;
  462. }
  463. /**
  464. * Set Pre-Calculate Formulas
  465. *
  466. * @param boolean $pValue Office2003 compatibility?
  467. * @return PHPExcel_Writer_Excel2007
  468. */
  469. public function setOffice2003Compatibility($pValue = false) {
  470. $this->_office2003compatibility = $pValue;
  471. return $this;
  472. }
  473. /**
  474. * Get use disk caching where possible?
  475. *
  476. * @return boolean
  477. */
  478. public function getUseDiskCaching() {
  479. return $this->_useDiskCaching;
  480. }
  481. /**
  482. * Set use disk caching where possible?
  483. *
  484. * @param boolean $pValue
  485. * @param string $pDirectory Disk caching directory
  486. * @throws Exception Exception when directory does not exist
  487. * @return PHPExcel_Writer_Excel2007
  488. */
  489. public function setUseDiskCaching($pValue = false, $pDirectory = null) {
  490. $this->_useDiskCaching = $pValue;
  491. if ($pDirectory !== NULL) {
  492. if (is_dir($pDirectory)) {
  493. $this->_diskCachingDirectory = $pDirectory;
  494. } else {
  495. throw new Exception("Directory does not exist: $pDirectory");
  496. }
  497. }
  498. return $this;
  499. }
  500. /**
  501. * Get disk caching directory
  502. *
  503. * @return string
  504. */
  505. public function getDiskCachingDirectory() {
  506. return $this->_diskCachingDirectory;
  507. }
  508. }