Subversion Repositories eFlore/Applications.cel

Rev

Rev 2388 | Details | Compare with Previous | Last modification | View Log | RSS feed

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