File Source for Excel2007.php

PHPExcel_Writer_Excel2007

# Source for file Excel2007.php

Documentation is available at Excel2007.php

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

Documentation generated on Sun, 27 Feb 2011 16:31:29 -0800 by phpDocumentor 1.4.3