Subversion Repositories eFlore/Applications.cel

Rev

Details | 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_HTML
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_HTML
31
 *
32
 * @category   PHPExcel
33
 * @package	PHPExcel_Writer_HTML
34
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
35
 */
36
class PHPExcel_Writer_HTML extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {
37
	/**
38
	 * PHPExcel object
39
	 *
40
	 * @var PHPExcel
41
	 */
42
	protected $_phpExcel;
43
 
44
	/**
45
	 * Sheet index to write
46
	 *
47
	 * @var int
48
	 */
49
	private $_sheetIndex	= 0;
50
 
51
	/**
52
	 * Images root
53
	 *
54
	 * @var string
55
	 */
56
	private $_imagesRoot	= '.';
57
 
58
	/**
59
	 * embed images, or link to images
60
	 *
61
	 * @var boolean
62
	 */
63
	private $_embedImages	= FALSE;
64
 
65
	/**
66
	 * Use inline CSS?
67
	 *
68
	 * @var boolean
69
	 */
70
	private $_useInlineCss = false;
71
 
72
	/**
73
	 * Array of CSS styles
74
	 *
75
	 * @var array
76
	 */
77
	private $_cssStyles = null;
78
 
79
	/**
80
	 * Array of column widths in points
81
	 *
82
	 * @var array
83
	 */
84
	private $_columnWidths = null;
85
 
86
	/**
87
	 * Default font
88
	 *
89
	 * @var PHPExcel_Style_Font
90
	 */
91
	private $_defaultFont;
92
 
93
	/**
94
	 * Flag whether spans have been calculated
95
	 *
96
	 * @var boolean
97
	 */
98
	private $_spansAreCalculated	= false;
99
 
100
	/**
101
	 * Excel cells that should not be written as HTML cells
102
	 *
103
	 * @var array
104
	 */
105
	private $_isSpannedCell	= array();
106
 
107
	/**
108
	 * Excel cells that are upper-left corner in a cell merge
109
	 *
110
	 * @var array
111
	 */
112
	private $_isBaseCell	= array();
113
 
114
	/**
115
	 * Excel rows that should not be written as HTML rows
116
	 *
117
	 * @var array
118
	 */
119
	private $_isSpannedRow	= array();
120
 
121
	/**
122
	 * Is the current writer creating PDF?
123
	 *
124
	 * @var boolean
125
	 */
126
	protected $_isPdf = false;
127
 
128
	/**
129
	 * Generate the Navigation block
130
	 *
131
	 * @var boolean
132
	 */
133
	private $_generateSheetNavigationBlock = true;
134
 
135
	/**
136
	 * Create a new PHPExcel_Writer_HTML
137
	 *
138
	 * @param	PHPExcel	$phpExcel	PHPExcel object
139
	 */
140
	public function __construct(PHPExcel $phpExcel) {
141
		$this->_phpExcel = $phpExcel;
142
		$this->_defaultFont = $this->_phpExcel->getDefaultStyle()->getFont();
143
	}
144
 
145
	/**
146
	 * Save PHPExcel to file
147
	 *
148
	 * @param	string		$pFilename
149
	 * @throws	PHPExcel_Writer_Exception
150
	 */
151
	public function save($pFilename = null) {
152
		// garbage collect
153
		$this->_phpExcel->garbageCollect();
154
 
155
		$saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
156
		PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
157
		$saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
158
		PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
159
 
160
		// Build CSS
161
		$this->buildCSS(!$this->_useInlineCss);
162
 
163
		// Open file
164
		$fileHandle = fopen($pFilename, 'wb+');
165
		if ($fileHandle === false) {
166
			throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
167
		}
168
 
169
		// Write headers
170
		fwrite($fileHandle, $this->generateHTMLHeader(!$this->_useInlineCss));
171
 
172
		// Write navigation (tabs)
173
		if ((!$this->_isPdf) && ($this->_generateSheetNavigationBlock)) {
174
			fwrite($fileHandle, $this->generateNavigation());
175
		}
176
 
177
		// Write data
178
		fwrite($fileHandle, $this->generateSheetData());
179
 
180
		// Write footer
181
		fwrite($fileHandle, $this->generateHTMLFooter());
182
 
183
		// Close file
184
		fclose($fileHandle);
185
 
186
		PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
187
		PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
188
	}
189
 
190
	/**
191
	 * Map VAlign
192
	 *
193
	 * @param	string		$vAlign		Vertical alignment
194
	 * @return string
195
	 */
196
	private function _mapVAlign($vAlign) {
197
		switch ($vAlign) {
198
			case PHPExcel_Style_Alignment::VERTICAL_BOTTOM:		return 'bottom';
199
			case PHPExcel_Style_Alignment::VERTICAL_TOP:		return 'top';
200
			case PHPExcel_Style_Alignment::VERTICAL_CENTER:
201
			case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY:	return 'middle';
202
			default: return 'baseline';
203
		}
204
	}
205
 
206
	/**
207
	 * Map HAlign
208
	 *
209
	 * @param	string		$hAlign		Horizontal alignment
210
	 * @return string|false
211
	 */
212
	private function _mapHAlign($hAlign) {
213
		switch ($hAlign) {
214
			case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:				return false;
215
			case PHPExcel_Style_Alignment::HORIZONTAL_LEFT:					return 'left';
216
			case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT:				return 'right';
217
			case PHPExcel_Style_Alignment::HORIZONTAL_CENTER:
218
			case PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS:	return 'center';
219
			case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY:				return 'justify';
220
			default: return false;
221
		}
222
	}
223
 
224
	/**
225
	 * Map border style
226
	 *
227
	 * @param	int		$borderStyle		Sheet index
228
	 * @return	string
229
	 */
230
	private function _mapBorderStyle($borderStyle) {
231
		switch ($borderStyle) {
232
			case PHPExcel_Style_Border::BORDER_NONE:				return 'none';
233
			case PHPExcel_Style_Border::BORDER_DASHDOT:				return '1px dashed';
234
			case PHPExcel_Style_Border::BORDER_DASHDOTDOT:			return '1px dotted';
235
			case PHPExcel_Style_Border::BORDER_DASHED:				return '1px dashed';
236
			case PHPExcel_Style_Border::BORDER_DOTTED:				return '1px dotted';
237
			case PHPExcel_Style_Border::BORDER_DOUBLE:				return '3px double';
238
			case PHPExcel_Style_Border::BORDER_HAIR:				return '1px solid';
239
			case PHPExcel_Style_Border::BORDER_MEDIUM:				return '2px solid';
240
			case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT:		return '2px dashed';
241
			case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT:	return '2px dotted';
242
			case PHPExcel_Style_Border::BORDER_MEDIUMDASHED:		return '2px dashed';
243
			case PHPExcel_Style_Border::BORDER_SLANTDASHDOT:		return '2px dashed';
244
			case PHPExcel_Style_Border::BORDER_THICK:				return '3px solid';
245
			case PHPExcel_Style_Border::BORDER_THIN:				return '1px solid';
246
			default: return '1px solid'; // map others to thin
247
		}
248
	}
249
 
250
	/**
251
	 * Get sheet index
252
	 *
253
	 * @return int
254
	 */
255
	public function getSheetIndex() {
256
		return $this->_sheetIndex;
257
	}
258
 
259
	/**
260
	 * Set sheet index
261
	 *
262
	 * @param	int		$pValue		Sheet index
263
	 * @return PHPExcel_Writer_HTML
264
	 */
265
	public function setSheetIndex($pValue = 0) {
266
		$this->_sheetIndex = $pValue;
267
		return $this;
268
	}
269
 
270
	/**
271
	 * Get sheet index
272
	 *
273
	 * @return boolean
274
	 */
275
	public function getGenerateSheetNavigationBlock() {
276
		return $this->_generateSheetNavigationBlock;
277
	}
278
 
279
	/**
280
	 * Set sheet index
281
	 *
282
	 * @param	boolean		$pValue		Flag indicating whether the sheet navigation block should be generated or not
283
	 * @return PHPExcel_Writer_HTML
284
	 */
285
	public function setGenerateSheetNavigationBlock($pValue = true) {
286
		$this->_generateSheetNavigationBlock = (bool) $pValue;
287
		return $this;
288
	}
289
 
290
	/**
291
	 * Write all sheets (resets sheetIndex to NULL)
292
	 */
293
	public function writeAllSheets() {
294
		$this->_sheetIndex = null;
295
		return $this;
296
	}
297
 
298
	/**
299
	 * Generate HTML header
300
	 *
301
	 * @param	boolean		$pIncludeStyles		Include styles?
302
	 * @return	string
303
	 * @throws PHPExcel_Writer_Exception
304
	 */
305
	public function generateHTMLHeader($pIncludeStyles = false) {
306
		// PHPExcel object known?
307
		if (is_null($this->_phpExcel)) {
308
			throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
309
		}
310
 
311
		// Construct HTML
312
		$properties = $this->_phpExcel->getProperties();
313
		$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' . PHP_EOL;
314
		$html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' . PHP_EOL;
315
		$html .= '<html>' . PHP_EOL;
316
		$html .= '  <head>' . PHP_EOL;
317
		$html .= '	  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . PHP_EOL;
318
		if ($properties->getTitle() > '')
319
			$html .= '	  <title>' . htmlspecialchars($properties->getTitle()) . '</title>' . PHP_EOL;
320
 
321
		if ($properties->getCreator() > '')
322
			$html .= '	  <meta name="author" content="' . htmlspecialchars($properties->getCreator()) . '" />' . PHP_EOL;
323
		if ($properties->getTitle() > '')
324
			$html .= '	  <meta name="title" content="' . htmlspecialchars($properties->getTitle()) . '" />' . PHP_EOL;
325
		if ($properties->getDescription() > '')
326
			$html .= '	  <meta name="description" content="' . htmlspecialchars($properties->getDescription()) . '" />' . PHP_EOL;
327
		if ($properties->getSubject() > '')
328
			$html .= '	  <meta name="subject" content="' . htmlspecialchars($properties->getSubject()) . '" />' . PHP_EOL;
329
		if ($properties->getKeywords() > '')
330
			$html .= '	  <meta name="keywords" content="' . htmlspecialchars($properties->getKeywords()) . '" />' . PHP_EOL;
331
		if ($properties->getCategory() > '')
332
			$html .= '	  <meta name="category" content="' . htmlspecialchars($properties->getCategory()) . '" />' . PHP_EOL;
333
		if ($properties->getCompany() > '')
334
			$html .= '	  <meta name="company" content="' . htmlspecialchars($properties->getCompany()) . '" />' . PHP_EOL;
335
		if ($properties->getManager() > '')
336
			$html .= '	  <meta name="manager" content="' . htmlspecialchars($properties->getManager()) . '" />' . PHP_EOL;
337
 
338
		if ($pIncludeStyles) {
339
			$html .= $this->generateStyles(true);
340
		}
341
 
342
		$html .= '  </head>' . PHP_EOL;
343
		$html .= '' . PHP_EOL;
344
		$html .= '  <body>' . PHP_EOL;
345
 
346
		// Return
347
		return $html;
348
	}
349
 
350
	/**
351
	 * Generate sheet data
352
	 *
353
	 * @return	string
354
	 * @throws PHPExcel_Writer_Exception
355
	 */
356
	public function generateSheetData() {
357
		// PHPExcel object known?
358
		if (is_null($this->_phpExcel)) {
359
			throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
360
		}
361
 
362
		// Ensure that Spans have been calculated?
363
		if (!$this->_spansAreCalculated) {
364
			$this->_calculateSpans();
365
		}
366
 
367
		// Fetch sheets
368
		$sheets = array();
369
		if (is_null($this->_sheetIndex)) {
370
			$sheets = $this->_phpExcel->getAllSheets();
371
		} else {
372
			$sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
373
		}
374
 
375
		// Construct HTML
376
		$html = '';
377
 
378
		// Loop all sheets
379
		$sheetId = 0;
380
		foreach ($sheets as $sheet) {
381
			// Write table header
382
			$html .= $this->_generateTableHeader($sheet);
383
 
384
			// Get worksheet dimension
385
			$dimension = explode(':', $sheet->calculateWorksheetDimension());
386
			$dimension[0] = PHPExcel_Cell::coordinateFromString($dimension[0]);
387
			$dimension[0][0] = PHPExcel_Cell::columnIndexFromString($dimension[0][0]) - 1;
388
			$dimension[1] = PHPExcel_Cell::coordinateFromString($dimension[1]);
389
			$dimension[1][0] = PHPExcel_Cell::columnIndexFromString($dimension[1][0]) - 1;
390
 
391
			// row min,max
392
			$rowMin = $dimension[0][1];
393
			$rowMax = $dimension[1][1];
394
 
395
			// calculate start of <tbody>, <thead>
396
			$tbodyStart = $rowMin;
397
			$theadStart = $theadEnd   = 0; // default: no <thead>	no </thead>
398
			if ($sheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
399
				$rowsToRepeatAtTop = $sheet->getPageSetup()->getRowsToRepeatAtTop();
400
 
401
				// we can only support repeating rows that start at top row
402
				if ($rowsToRepeatAtTop[0] == 1) {
403
					$theadStart = $rowsToRepeatAtTop[0];
404
					$theadEnd   = $rowsToRepeatAtTop[1];
405
					$tbodyStart = $rowsToRepeatAtTop[1] + 1;
406
				}
407
			}
408
 
409
			// Loop through cells
410
			$row = $rowMin-1;
411
			while($row++ < $rowMax) {
412
				// <thead> ?
413
				if ($row == $theadStart) {
414
					$html .= '		<thead>' . PHP_EOL;
415
				}
416
 
417
				// <tbody> ?
418
				if ($row == $tbodyStart) {
419
					$html .= '		<tbody>' . PHP_EOL;
420
				}
421
 
422
				// Write row if there are HTML table cells in it
423
				if ( !isset($this->_isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row]) ) {
424
					// Start a new rowData
425
					$rowData = array();
426
					// Loop through columns
427
					$column = $dimension[0][0] - 1;
428
					while($column++ < $dimension[1][0]) {
429
						// Cell exists?
430
						if ($sheet->cellExistsByColumnAndRow($column, $row)) {
431
							$rowData[$column] = $sheet->getCellByColumnAndRow($column, $row);
432
						} else {
433
							$rowData[$column] = '';
434
						}
435
					}
436
					$html .= $this->_generateRow($sheet, $rowData, $row - 1);
437
				}
438
 
439
				// </thead> ?
440
				if ($row == $theadEnd) {
441
					$html .= '		</thead>' . PHP_EOL;
442
				}
443
			}
444
			$html .= $this->_extendRowsForChartsAndImages($sheet, $row);
445
 
446
			// Close table body.
447
			$html .= '		</tbody>' . PHP_EOL;
448
 
449
			// Write table footer
450
			$html .= $this->_generateTableFooter();
451
 
452
			// Writing PDF?
453
			if ($this->_isPdf) {
454
				if (is_null($this->_sheetIndex) && $sheetId + 1 < $this->_phpExcel->getSheetCount()) {
455
					$html .= '<div style="page-break-before:always" />';
456
				}
457
			}
458
 
459
			// Next sheet
460
			++$sheetId;
461
		}
462
 
463
		// Return
464
		return $html;
465
	}
466
 
467
	/**
468
	 * Generate sheet tabs
469
	 *
470
	 * @return	string
471
	 * @throws PHPExcel_Writer_Exception
472
	 */
473
	public function generateNavigation()
474
	{
475
		// PHPExcel object known?
476
		if (is_null($this->_phpExcel)) {
477
			throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
478
		}
479
 
480
		// Fetch sheets
481
		$sheets = array();
482
		if (is_null($this->_sheetIndex)) {
483
			$sheets = $this->_phpExcel->getAllSheets();
484
		} else {
485
			$sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
486
		}
487
 
488
		// Construct HTML
489
		$html = '';
490
 
491
		// Only if there are more than 1 sheets
492
		if (count($sheets) > 1) {
493
			// Loop all sheets
494
			$sheetId = 0;
495
 
496
			$html .= '<ul class="navigation">' . PHP_EOL;
497
 
498
			foreach ($sheets as $sheet) {
499
				$html .= '  <li class="sheet' . $sheetId . '"><a href="#sheet' . $sheetId . '">' . $sheet->getTitle() . '</a></li>' . PHP_EOL;
500
				++$sheetId;
501
			}
502
 
503
			$html .= '</ul>' . PHP_EOL;
504
		}
505
 
506
		return $html;
507
	}
508
 
509
	private function _extendRowsForChartsAndImages(PHPExcel_Worksheet $pSheet, $row) {
510
		$rowMax = $row;
511
		$colMax = 'A';
512
		if ($this->_includeCharts) {
513
			foreach ($pSheet->getChartCollection() as $chart) {
514
				if ($chart instanceof PHPExcel_Chart) {
515
				    $chartCoordinates = $chart->getTopLeftPosition();
516
				    $chartTL = PHPExcel_Cell::coordinateFromString($chartCoordinates['cell']);
517
					$chartCol = PHPExcel_Cell::columnIndexFromString($chartTL[0]);
518
					if ($chartTL[1] > $rowMax) {
519
						$rowMax = $chartTL[1];
520
						if ($chartCol > PHPExcel_Cell::columnIndexFromString($colMax)) {
521
							$colMax = $chartTL[0];
522
						}
523
					}
524
				}
525
			}
526
		}
527
 
528
		foreach ($pSheet->getDrawingCollection() as $drawing) {
529
			if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
530
			    $imageTL = PHPExcel_Cell::coordinateFromString($drawing->getCoordinates());
531
				$imageCol = PHPExcel_Cell::columnIndexFromString($imageTL[0]);
532
				if ($imageTL[1] > $rowMax) {
533
					$rowMax = $imageTL[1];
534
					if ($imageCol > PHPExcel_Cell::columnIndexFromString($colMax)) {
535
						$colMax = $imageTL[0];
536
					}
537
				}
538
			}
539
		}
540
		$html = '';
541
		$colMax++;
542
		while ($row < $rowMax) {
543
			$html .= '<tr>';
544
			for ($col = 'A'; $col != $colMax; ++$col) {
545
				$html .= '<td>';
546
				$html .= $this->_writeImageInCell($pSheet, $col.$row);
547
				if ($this->_includeCharts) {
548
					$html .= $this->_writeChartInCell($pSheet, $col.$row);
549
				}
550
				$html .= '</td>';
551
			}
552
			++$row;
553
			$html .= '</tr>';
554
		}
555
		return $html;
556
	}
557
 
558
 
559
	/**
560
	 * Generate image tag in cell
561
	 *
562
	 * @param	PHPExcel_Worksheet	$pSheet			PHPExcel_Worksheet
563
	 * @param	string				$coordinates	Cell coordinates
564
	 * @return	string
565
	 * @throws	PHPExcel_Writer_Exception
566
	 */
567
	private function _writeImageInCell(PHPExcel_Worksheet $pSheet, $coordinates) {
568
		// Construct HTML
569
		$html = '';
570
 
571
		// Write images
572
		foreach ($pSheet->getDrawingCollection() as $drawing) {
573
			if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
574
				if ($drawing->getCoordinates() == $coordinates) {
575
					$filename = $drawing->getPath();
576
 
577
					// Strip off eventual '.'
578
					if (substr($filename, 0, 1) == '.') {
579
						$filename = substr($filename, 1);
580
					}
581
 
582
					// Prepend images root
583
					$filename = $this->getImagesRoot() . $filename;
584
 
585
					// Strip off eventual '.'
586
					if (substr($filename, 0, 1) == '.' && substr($filename, 0, 2) != './') {
587
						$filename = substr($filename, 1);
588
					}
589
 
590
					// Convert UTF8 data to PCDATA
591
					$filename = htmlspecialchars($filename);
592
 
593
					$html .= PHP_EOL;
594
					if ((!$this->_embedImages) || ($this->_isPdf)) {
595
						$imageData = $filename;
596
					} else {
597
						$imageDetails = getimagesize($filename);
598
						if ($fp = fopen($filename,"rb", 0)) {
599
							$picture = fread($fp,filesize($filename));
600
							fclose($fp);
601
							// base64 encode the binary data, then break it
602
							// into chunks according to RFC 2045 semantics
603
							$base64 = chunk_split(base64_encode($picture));
604
							$imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64;
605
						} else {
606
							$imageData = $filename;
607
						}
608
					}
609
 
610
					$html .= '<div style="position: relative;">';
611
					$html .= '<img style="position: absolute; z-index: 1; left: ' . $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' . $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' . $imageData . '" border="0" />' . PHP_EOL;
612
					$html .= '</div>';
613
				}
614
			}
615
		}
616
 
617
		// Return
618
		return $html;
619
	}
620
 
621
	/**
622
	 * Generate chart tag in cell
623
	 *
624
	 * @param	PHPExcel_Worksheet	$pSheet			PHPExcel_Worksheet
625
	 * @param	string				$coordinates	Cell coordinates
626
	 * @return	string
627
	 * @throws	PHPExcel_Writer_Exception
628
	 */
629
	private function _writeChartInCell(PHPExcel_Worksheet $pSheet, $coordinates) {
630
		// Construct HTML
631
		$html = '';
632
 
633
		// Write charts
634
		foreach ($pSheet->getChartCollection() as $chart) {
635
			if ($chart instanceof PHPExcel_Chart) {
636
			    $chartCoordinates = $chart->getTopLeftPosition();
637
				if ($chartCoordinates['cell'] == $coordinates) {
638
					$chartFileName = PHPExcel_Shared_File::sys_get_temp_dir().'/'.uniqid().'.png';
639
					if (!$chart->render($chartFileName)) {
640
						return;
641
					}
642
 
643
					$html .= PHP_EOL;
644
					$imageDetails = getimagesize($chartFileName);
645
					if ($fp = fopen($chartFileName,"rb", 0)) {
646
						$picture = fread($fp,filesize($chartFileName));
647
						fclose($fp);
648
						// base64 encode the binary data, then break it
649
						// into chunks according to RFC 2045 semantics
650
						$base64 = chunk_split(base64_encode($picture));
651
						$imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64;
652
 
653
						$html .= '<div style="position: relative;">';
654
						$html .= '<img style="position: absolute; z-index: 1; left: ' . $chartCoordinates['xOffset'] . 'px; top: ' . $chartCoordinates['yOffset'] . 'px; width: ' . $imageDetails[0] . 'px; height: ' . $imageDetails[1] . 'px;" src="' . $imageData . '" border="0" />' . PHP_EOL;
655
						$html .= '</div>';
656
 
657
						unlink($chartFileName);
658
					}
659
				}
660
			}
661
		}
662
 
663
		// Return
664
		return $html;
665
	}
666
 
667
	/**
668
	 * Generate CSS styles
669
	 *
670
	 * @param	boolean	$generateSurroundingHTML	Generate surrounding HTML tags? (<style> and </style>)
671
	 * @return	string
672
	 * @throws	PHPExcel_Writer_Exception
673
	 */
674
	public function generateStyles($generateSurroundingHTML = true) {
675
		// PHPExcel object known?
676
		if (is_null($this->_phpExcel)) {
677
			throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
678
		}
679
 
680
		// Build CSS
681
		$css = $this->buildCSS($generateSurroundingHTML);
682
 
683
		// Construct HTML
684
		$html = '';
685
 
686
		// Start styles
687
		if ($generateSurroundingHTML) {
688
			$html .= '	<style type="text/css">' . PHP_EOL;
689
			$html .= '	  html { ' . $this->_assembleCSS($css['html']) . ' }' . PHP_EOL;
690
		}
691
 
692
		// Write all other styles
693
		foreach ($css as $styleName => $styleDefinition) {
694
			if ($styleName != 'html') {
695
				$html .= '	  ' . $styleName . ' { ' . $this->_assembleCSS($styleDefinition) . ' }' . PHP_EOL;
696
			}
697
		}
698
 
699
		// End styles
700
		if ($generateSurroundingHTML) {
701
			$html .= '	</style>' . PHP_EOL;
702
		}
703
 
704
		// Return
705
		return $html;
706
	}
707
 
708
	/**
709
	 * Build CSS styles
710
	 *
711
	 * @param	boolean	$generateSurroundingHTML	Generate surrounding HTML style? (html { })
712
	 * @return	array
713
	 * @throws	PHPExcel_Writer_Exception
714
	 */
715
	public function buildCSS($generateSurroundingHTML = true) {
716
		// PHPExcel object known?
717
		if (is_null($this->_phpExcel)) {
718
			throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
719
		}
720
 
721
		// Cached?
722
		if (!is_null($this->_cssStyles)) {
723
			return $this->_cssStyles;
724
		}
725
 
726
		// Ensure that spans have been calculated
727
		if (!$this->_spansAreCalculated) {
728
			$this->_calculateSpans();
729
		}
730
 
731
		// Construct CSS
732
		$css = array();
733
 
734
		// Start styles
735
		if ($generateSurroundingHTML) {
736
			// html { }
737
			$css['html']['font-family']	  = 'Calibri, Arial, Helvetica, sans-serif';
738
			$css['html']['font-size']		= '11pt';
739
			$css['html']['background-color'] = 'white';
740
		}
741
 
742
 
743
		// table { }
744
		$css['table']['border-collapse']  = 'collapse';
745
	    if (!$this->_isPdf) {
746
			$css['table']['page-break-after'] = 'always';
747
		}
748
 
749
		// .gridlines td { }
750
		$css['.gridlines td']['border'] = '1px dotted black';
751
 
752
		// .b {}
753
		$css['.b']['text-align'] = 'center'; // BOOL
754
 
755
		// .e {}
756
		$css['.e']['text-align'] = 'center'; // ERROR
757
 
758
		// .f {}
759
		$css['.f']['text-align'] = 'right'; // FORMULA
760
 
761
		// .inlineStr {}
762
		$css['.inlineStr']['text-align'] = 'left'; // INLINE
763
 
764
		// .n {}
765
		$css['.n']['text-align'] = 'right'; // NUMERIC
766
 
767
		// .s {}
768
		$css['.s']['text-align'] = 'left'; // STRING
769
 
770
		// Calculate cell style hashes
771
		foreach ($this->_phpExcel->getCellXfCollection() as $index => $style) {
772
			$css['td.style' . $index] = $this->_createCSSStyle( $style );
773
		}
774
 
775
		// Fetch sheets
776
		$sheets = array();
777
		if (is_null($this->_sheetIndex)) {
778
			$sheets = $this->_phpExcel->getAllSheets();
779
		} else {
780
			$sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
781
		}
782
 
783
		// Build styles per sheet
784
		foreach ($sheets as $sheet) {
785
			// Calculate hash code
786
			$sheetIndex = $sheet->getParent()->getIndex($sheet);
787
 
788
			// Build styles
789
			// Calculate column widths
790
			$sheet->calculateColumnWidths();
791
 
792
			// col elements, initialize
793
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()) - 1;
794
			$column = -1;
795
			while($column++ < $highestColumnIndex) {
796
				$this->_columnWidths[$sheetIndex][$column] = 42; // approximation
797
				$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = '42pt';
798
			}
799
 
800
			// col elements, loop through columnDimensions and set width
801
			foreach ($sheet->getColumnDimensions() as $columnDimension) {
802
				if (($width = PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth(), $this->_defaultFont)) >= 0) {
803
					$width = PHPExcel_Shared_Drawing::pixelsToPoints($width);
804
					$column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
805
					$this->_columnWidths[$sheetIndex][$column] = $width;
806
					$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt';
807
 
808
					if ($columnDimension->getVisible() === false) {
809
						$css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse';
810
						$css['table.sheet' . $sheetIndex . ' col.col' . $column]['*display'] = 'none'; // target IE6+7
811
					}
812
				}
813
			}
814
 
815
			// Default row height
816
			$rowDimension = $sheet->getDefaultRowDimension();
817
 
818
			// table.sheetN tr { }
819
			$css['table.sheet' . $sheetIndex . ' tr'] = array();
820
 
821
			if ($rowDimension->getRowHeight() == -1) {
822
				$pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
823
			} else {
824
				$pt_height = $rowDimension->getRowHeight();
825
			}
826
			$css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt';
827
			if ($rowDimension->getVisible() === false) {
828
				$css['table.sheet' . $sheetIndex . ' tr']['display']	= 'none';
829
				$css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden';
830
			}
831
 
832
			// Calculate row heights
833
			foreach ($sheet->getRowDimensions() as $rowDimension) {
834
				$row = $rowDimension->getRowIndex() - 1;
835
 
836
				// table.sheetN tr.rowYYYYYY { }
837
				$css['table.sheet' . $sheetIndex . ' tr.row' . $row] = array();
838
 
839
				if ($rowDimension->getRowHeight() == -1) {
840
					$pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
841
				} else {
842
					$pt_height = $rowDimension->getRowHeight();
843
				}
844
				$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt';
845
				if ($rowDimension->getVisible() === false) {
846
					$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none';
847
					$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden';
848
				}
849
			}
850
		}
851
 
852
		// Cache
853
		if (is_null($this->_cssStyles)) {
854
			$this->_cssStyles = $css;
855
		}
856
 
857
		// Return
858
		return $css;
859
	}
860
 
861
	/**
862
	 * Create CSS style
863
	 *
864
	 * @param	PHPExcel_Style		$pStyle			PHPExcel_Style
865
	 * @return	array
866
	 */
867
	private function _createCSSStyle(PHPExcel_Style $pStyle) {
868
		// Construct CSS
869
		$css = '';
870
 
871
		// Create CSS
872
		$css = array_merge(
873
			$this->_createCSSStyleAlignment($pStyle->getAlignment())
874
			, $this->_createCSSStyleBorders($pStyle->getBorders())
875
			, $this->_createCSSStyleFont($pStyle->getFont())
876
			, $this->_createCSSStyleFill($pStyle->getFill())
877
		);
878
 
879
		// Return
880
		return $css;
881
	}
882
 
883
	/**
884
	 * Create CSS style (PHPExcel_Style_Alignment)
885
	 *
886
	 * @param	PHPExcel_Style_Alignment		$pStyle			PHPExcel_Style_Alignment
887
	 * @return	array
888
	 */
889
	private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle) {
890
		// Construct CSS
891
		$css = array();
892
 
893
		// Create CSS
894
		$css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
895
		if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) {
896
			$css['text-align'] = $textAlign;
897
			if(in_array($textAlign,array('left','right')))
898
				$css['padding-'.$textAlign] = (string)((int)$pStyle->getIndent() * 9).'px';
899
		}
900
 
901
		// Return
902
		return $css;
903
	}
904
 
905
	/**
906
	 * Create CSS style (PHPExcel_Style_Font)
907
	 *
908
	 * @param	PHPExcel_Style_Font		$pStyle			PHPExcel_Style_Font
909
	 * @return	array
910
	 */
911
	private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle) {
912
		// Construct CSS
913
		$css = array();
914
 
915
		// Create CSS
916
		if ($pStyle->getBold()) {
917
			$css['font-weight'] = 'bold';
918
		}
919
		if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
920
			$css['text-decoration'] = 'underline line-through';
921
		} else if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
922
			$css['text-decoration'] = 'underline';
923
		} else if ($pStyle->getStrikethrough()) {
924
			$css['text-decoration'] = 'line-through';
925
		}
926
		if ($pStyle->getItalic()) {
927
			$css['font-style'] = 'italic';
928
		}
929
 
930
		$css['color']		= '#' . $pStyle->getColor()->getRGB();
931
		$css['font-family']	= '\'' . $pStyle->getName() . '\'';
932
		$css['font-size']	= $pStyle->getSize() . 'pt';
933
 
934
		// Return
935
		return $css;
936
	}
937
 
938
	/**
939
	 * Create CSS style (PHPExcel_Style_Borders)
940
	 *
941
	 * @param	PHPExcel_Style_Borders		$pStyle			PHPExcel_Style_Borders
942
	 * @return	array
943
	 */
944
	private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle) {
945
		// Construct CSS
946
		$css = array();
947
 
948
		// Create CSS
949
		$css['border-bottom']	= $this->_createCSSStyleBorder($pStyle->getBottom());
950
		$css['border-top']		= $this->_createCSSStyleBorder($pStyle->getTop());
951
		$css['border-left']		= $this->_createCSSStyleBorder($pStyle->getLeft());
952
		$css['border-right']	= $this->_createCSSStyleBorder($pStyle->getRight());
953
 
954
		// Return
955
		return $css;
956
	}
957
 
958
	/**
959
	 * Create CSS style (PHPExcel_Style_Border)
960
	 *
961
	 * @param	PHPExcel_Style_Border		$pStyle			PHPExcel_Style_Border
962
	 * @return	string
963
	 */
964
	private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle) {
965
		// Create CSS
966
//		$css = $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
967
		//	Create CSS - add !important to non-none border styles for merged cells
968
		$borderStyle = $this->_mapBorderStyle($pStyle->getBorderStyle());
969
		$css = $borderStyle . ' #' . $pStyle->getColor()->getRGB() . (($borderStyle == 'none') ? '' : ' !important');
970
 
971
		// Return
972
		return $css;
973
	}
974
 
975
	/**
976
	 * Create CSS style (PHPExcel_Style_Fill)
977
	 *
978
	 * @param	PHPExcel_Style_Fill		$pStyle			PHPExcel_Style_Fill
979
	 * @return	array
980
	 */
981
	private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle) {
982
		// Construct HTML
983
		$css = array();
984
 
985
		// Create CSS
986
		$value = $pStyle->getFillType() == PHPExcel_Style_Fill::FILL_NONE ?
987
			'white' : '#' . $pStyle->getStartColor()->getRGB();
988
		$css['background-color'] = $value;
989
 
990
		// Return
991
		return $css;
992
	}
993
 
994
	/**
995
	 * Generate HTML footer
996
	 */
997
	public function generateHTMLFooter() {
998
		// Construct HTML
999
		$html = '';
1000
		$html .= '  </body>' . PHP_EOL;
1001
		$html .= '</html>' . PHP_EOL;
1002
 
1003
		// Return
1004
		return $html;
1005
	}
1006
 
1007
	/**
1008
	 * Generate table header
1009
	 *
1010
	 * @param	PHPExcel_Worksheet	$pSheet		The worksheet for the table we are writing
1011
	 * @return	string
1012
	 * @throws	PHPExcel_Writer_Exception
1013
	 */
1014
	private function _generateTableHeader($pSheet) {
1015
		$sheetIndex = $pSheet->getParent()->getIndex($pSheet);
1016
 
1017
		// Construct HTML
1018
		$html = '';
1019
		$html .= $this->_setMargins($pSheet);
1020
 
1021
		if (!$this->_useInlineCss) {
1022
			$gridlines = $pSheet->getShowGridLines() ? ' gridlines' : '';
1023
			$html .= '	<table border="0" cellpadding="0" cellspacing="0" id="sheet' . $sheetIndex . '" class="sheet' . $sheetIndex . $gridlines . '">' . PHP_EOL;
1024
		} else {
1025
			$style = isset($this->_cssStyles['table']) ?
1026
				$this->_assembleCSS($this->_cssStyles['table']) : '';
1027
 
1028
			if ($this->_isPdf && $pSheet->getShowGridLines()) {
1029
				$html .= '	<table border="1" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="1" style="' . $style . '">' . PHP_EOL;
1030
			} else {
1031
				$html .= '	<table border="0" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="0" style="' . $style . '">' . PHP_EOL;
1032
			}
1033
		}
1034
 
1035
		// Write <col> elements
1036
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn()) - 1;
1037
		$i = -1;
1038
		while($i++ < $highestColumnIndex) {
1039
		    if (!$this->_isPdf) {
1040
				if (!$this->_useInlineCss) {
1041
					$html .= '		<col class="col' . $i . '">' . PHP_EOL;
1042
				} else {
1043
					$style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) ?
1044
						$this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) : '';
1045
					$html .= '		<col style="' . $style . '">' . PHP_EOL;
1046
				}
1047
			}
1048
		}
1049
 
1050
		// Return
1051
		return $html;
1052
	}
1053
 
1054
	/**
1055
	 * Generate table footer
1056
	 *
1057
	 * @throws	PHPExcel_Writer_Exception
1058
	 */
1059
	private function _generateTableFooter() {
1060
		// Construct HTML
1061
		$html = '';
1062
		$html .= '	</table>' . PHP_EOL;
1063
 
1064
		// Return
1065
		return $html;
1066
	}
1067
 
1068
	/**
1069
	 * Generate row
1070
	 *
1071
	 * @param	PHPExcel_Worksheet	$pSheet			PHPExcel_Worksheet
1072
	 * @param	array				$pValues		Array containing cells in a row
1073
	 * @param	int					$pRow			Row number (0-based)
1074
	 * @return	string
1075
	 * @throws	PHPExcel_Writer_Exception
1076
	 */
1077
	private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
1078
		if (is_array($pValues)) {
1079
			// Construct HTML
1080
			$html = '';
1081
 
1082
			// Sheet index
1083
			$sheetIndex = $pSheet->getParent()->getIndex($pSheet);
1084
 
1085
			// DomPDF and breaks
1086
			if ($this->_isPdf && count($pSheet->getBreaks()) > 0) {
1087
				$breaks = $pSheet->getBreaks();
1088
 
1089
				// check if a break is needed before this row
1090
				if (isset($breaks['A' . $pRow])) {
1091
					// close table: </table>
1092
					$html .= $this->_generateTableFooter();
1093
 
1094
					// insert page break
1095
					$html .= '<div style="page-break-before:always" />';
1096
 
1097
					// open table again: <table> + <col> etc.
1098
					$html .= $this->_generateTableHeader($pSheet);
1099
				}
1100
			}
1101
 
1102
			// Write row start
1103
			if (!$this->_useInlineCss) {
1104
				$html .= '		  <tr class="row' . $pRow . '">' . PHP_EOL;
1105
			} else {
1106
				$style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow])
1107
					? $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
1108
 
1109
				$html .= '		  <tr style="' . $style . '">' . PHP_EOL;
1110
			}
1111
 
1112
			// Write cells
1113
			$colNum = 0;
1114
			foreach ($pValues as $cell) {
1115
				$coordinate = PHPExcel_Cell::stringFromColumnIndex($colNum) . ($pRow + 1);
1116
 
1117
				if (!$this->_useInlineCss) {
1118
					$cssClass = '';
1119
					$cssClass = 'column' . $colNum;
1120
				} else {
1121
					$cssClass = array();
1122
					if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
1123
						$this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
1124
					}
1125
				}
1126
				$colSpan = 1;
1127
				$rowSpan = 1;
1128
 
1129
				// initialize
1130
				$cellData = '&nbsp;';
1131
 
1132
				// PHPExcel_Cell
1133
				if ($cell instanceof PHPExcel_Cell) {
1134
					$cellData = '';
1135
					if (is_null($cell->getParent())) {
1136
						$cell->attach($pSheet);
1137
					}
1138
					// Value
1139
					if ($cell->getValue() instanceof PHPExcel_RichText) {
1140
						// Loop through rich text elements
1141
						$elements = $cell->getValue()->getRichTextElements();
1142
						foreach ($elements as $element) {
1143
							// Rich text start?
1144
							if ($element instanceof PHPExcel_RichText_Run) {
1145
								$cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';
1146
 
1147
								if ($element->getFont()->getSuperScript()) {
1148
									$cellData .= '<sup>';
1149
								} else if ($element->getFont()->getSubScript()) {
1150
									$cellData .= '<sub>';
1151
								}
1152
							}
1153
 
1154
							// Convert UTF8 data to PCDATA
1155
							$cellText = $element->getText();
1156
							$cellData .= htmlspecialchars($cellText);
1157
 
1158
							if ($element instanceof PHPExcel_RichText_Run) {
1159
								if ($element->getFont()->getSuperScript()) {
1160
									$cellData .= '</sup>';
1161
								} else if ($element->getFont()->getSubScript()) {
1162
									$cellData .= '</sub>';
1163
								}
1164
 
1165
								$cellData .= '</span>';
1166
							}
1167
						}
1168
					} else {
1169
						if ($this->_preCalculateFormulas) {
1170
							$cellData = PHPExcel_Style_NumberFormat::toFormattedString(
1171
								$cell->getCalculatedValue(),
1172
								$pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
1173
								array($this, 'formatColor')
1174
							);
1175
						} else {
1176
							$cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
1177
								$cell->getValue(),
1178
								$pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
1179
								array($this, 'formatColor')
1180
							);
1181
						}
1182
						$cellData = htmlspecialchars($cellData);
1183
						if ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSuperScript()) {
1184
							$cellData = '<sup>'.$cellData.'</sup>';
1185
						} elseif ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSubScript()) {
1186
							$cellData = '<sub>'.$cellData.'</sub>';
1187
						}
1188
					}
1189
 
1190
					// Converts the cell content so that spaces occuring at beginning of each new line are replaced by &nbsp;
1191
					// Example: "  Hello\n to the world" is converted to "&nbsp;&nbsp;Hello\n&nbsp;to the world"
1192
					$cellData = preg_replace("/(?m)(?:^|\\G) /", '&nbsp;', $cellData);
1193
 
1194
					// convert newline "\n" to '<br>'
1195
					$cellData = nl2br($cellData);
1196
 
1197
					// Extend CSS class?
1198
					if (!$this->_useInlineCss) {
1199
						$cssClass .= ' style' . $cell->getXfIndex();
1200
						$cssClass .= ' ' . $cell->getDataType();
1201
					} else {
1202
						if (isset($this->_cssStyles['td.style' . $cell->getXfIndex()])) {
1203
							$cssClass = array_merge($cssClass, $this->_cssStyles['td.style' . $cell->getXfIndex()]);
1204
						}
1205
 
1206
						// General horizontal alignment: Actual horizontal alignment depends on dataType
1207
						$sharedStyle = $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() );
1208
						if ($sharedStyle->getAlignment()->getHorizontal() == PHPExcel_Style_Alignment::HORIZONTAL_GENERAL
1209
							&& isset($this->_cssStyles['.' . $cell->getDataType()]['text-align']))
1210
						{
1211
							$cssClass['text-align'] = $this->_cssStyles['.' . $cell->getDataType()]['text-align'];
1212
						}
1213
					}
1214
				}
1215
 
1216
				// Hyperlink?
1217
				if ($pSheet->hyperlinkExists($coordinate) && !$pSheet->getHyperlink($coordinate)->isInternal()) {
1218
					$cellData = '<a href="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getUrl()) . '" title="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getTooltip()) . '">' . $cellData . '</a>';
1219
				}
1220
 
1221
				// Should the cell be written or is it swallowed by a rowspan or colspan?
1222
				$writeCell = ! ( isset($this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])
1223
							&& $this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum] );
1224
 
1225
				// Colspan and Rowspan
1226
				$colspan = 1;
1227
				$rowspan = 1;
1228
				if (isset($this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])) {
1229
					$spans = $this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum];
1230
					$rowSpan = $spans['rowspan'];
1231
					$colSpan = $spans['colspan'];
1232
 
1233
					//	Also apply style from last cell in merge to fix borders -
1234
					//		relies on !important for non-none border declarations in _createCSSStyleBorder
1235
					$endCellCoord = PHPExcel_Cell::stringFromColumnIndex($colNum + $colSpan - 1) . ($pRow + $rowSpan);
1236
					$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
1237
				}
1238
 
1239
				// Write
1240
				if ($writeCell) {
1241
					// Column start
1242
					$html .= '			<td';
1243
						if (!$this->_useInlineCss) {
1244
							$html .= ' class="' . $cssClass . '"';
1245
						} else {
1246
							//** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
1247
							// We must explicitly write the width of the <td> element because TCPDF
1248
							// does not recognize e.g. <col style="width:42pt">
1249
							$width = 0;
1250
							$i = $colNum - 1;
1251
							$e = $colNum + $colSpan - 1;
1252
							while($i++ < $e) {
1253
								if (isset($this->_columnWidths[$sheetIndex][$i])) {
1254
									$width += $this->_columnWidths[$sheetIndex][$i];
1255
								}
1256
							}
1257
							$cssClass['width'] = $width . 'pt';
1258
 
1259
							// We must also explicitly write the height of the <td> element because TCPDF
1260
							// does not recognize e.g. <tr style="height:50pt">
1261
							if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
1262
								$height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
1263
								$cssClass['height'] = $height;
1264
							}
1265
							//** end of redundant code **
1266
 
1267
							$html .= ' style="' . $this->_assembleCSS($cssClass) . '"';
1268
						}
1269
						if ($colSpan > 1) {
1270
							$html .= ' colspan="' . $colSpan . '"';
1271
						}
1272
						if ($rowSpan > 1) {
1273
							$html .= ' rowspan="' . $rowSpan . '"';
1274
						}
1275
					$html .= '>';
1276
 
1277
					// Image?
1278
					$html .= $this->_writeImageInCell($pSheet, $coordinate);
1279
 
1280
					// Chart?
1281
					if ($this->_includeCharts) {
1282
						$html .= $this->_writeChartInCell($pSheet, $coordinate);
1283
					}
1284
 
1285
					// Cell data
1286
					$html .= $cellData;
1287
 
1288
					// Column end
1289
					$html .= '</td>' . PHP_EOL;
1290
				}
1291
 
1292
				// Next column
1293
				++$colNum;
1294
			}
1295
 
1296
			// Write row end
1297
			$html .= '		  </tr>' . PHP_EOL;
1298
 
1299
			// Return
1300
			return $html;
1301
		} else {
1302
			throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
1303
		}
1304
	}
1305
 
1306
	/**
1307
	 * Takes array where of CSS properties / values and converts to CSS string
1308
	 *
1309
	 * @param array
1310
	 * @return string
1311
	 */
1312
	private function _assembleCSS($pValue = array())
1313
	{
1314
		$pairs = array();
1315
		foreach ($pValue as $property => $value) {
1316
			$pairs[] = $property . ':' . $value;
1317
		}
1318
		$string = implode('; ', $pairs);
1319
 
1320
		return $string;
1321
	}
1322
 
1323
	/**
1324
	 * Get images root
1325
	 *
1326
	 * @return string
1327
	 */
1328
	public function getImagesRoot() {
1329
		return $this->_imagesRoot;
1330
	}
1331
 
1332
	/**
1333
	 * Set images root
1334
	 *
1335
	 * @param string $pValue
1336
	 * @return PHPExcel_Writer_HTML
1337
	 */
1338
	public function setImagesRoot($pValue = '.') {
1339
		$this->_imagesRoot = $pValue;
1340
		return $this;
1341
	}
1342
 
1343
	/**
1344
	 * Get embed images
1345
	 *
1346
	 * @return boolean
1347
	 */
1348
	public function getEmbedImages() {
1349
		return $this->_embedImages;
1350
	}
1351
 
1352
	/**
1353
	 * Set embed images
1354
	 *
1355
	 * @param boolean $pValue
1356
	 * @return PHPExcel_Writer_HTML
1357
	 */
1358
	public function setEmbedImages($pValue = '.') {
1359
		$this->_embedImages = $pValue;
1360
		return $this;
1361
	}
1362
 
1363
	/**
1364
	 * Get use inline CSS?
1365
	 *
1366
	 * @return boolean
1367
	 */
1368
	public function getUseInlineCss() {
1369
		return $this->_useInlineCss;
1370
	}
1371
 
1372
	/**
1373
	 * Set use inline CSS?
1374
	 *
1375
	 * @param boolean $pValue
1376
	 * @return PHPExcel_Writer_HTML
1377
	 */
1378
	public function setUseInlineCss($pValue = false) {
1379
		$this->_useInlineCss = $pValue;
1380
		return $this;
1381
	}
1382
 
1383
	/**
1384
	 * Add color to formatted string as inline style
1385
	 *
1386
	 * @param string $pValue Plain formatted value without color
1387
	 * @param string $pFormat Format code
1388
	 * @return string
1389
	 */
1390
	public function formatColor($pValue, $pFormat)
1391
	{
1392
		// Color information, e.g. [Red] is always at the beginning
1393
		$color = null; // initialize
1394
		$matches = array();
1395
 
1396
		$color_regex = '/^\\[[a-zA-Z]+\\]/';
1397
		if (preg_match($color_regex, $pFormat, $matches)) {
1398
			$color = str_replace('[', '', $matches[0]);
1399
			$color = str_replace(']', '', $color);
1400
			$color = strtolower($color);
1401
		}
1402
 
1403
		// convert to PCDATA
1404
		$value = htmlspecialchars($pValue);
1405
 
1406
		// color span tag
1407
		if ($color !== null) {
1408
			$value = '<span style="color:' . $color . '">' . $value . '</span>';
1409
		}
1410
 
1411
		return $value;
1412
	}
1413
 
1414
	/**
1415
	 * Calculate information about HTML colspan and rowspan which is not always the same as Excel's
1416
	 */
1417
	private function _calculateSpans()
1418
	{
1419
		// Identify all cells that should be omitted in HTML due to cell merge.
1420
		// In HTML only the upper-left cell should be written and it should have
1421
		//   appropriate rowspan / colspan attribute
1422
		$sheetIndexes = $this->_sheetIndex !== null ?
1423
			array($this->_sheetIndex) : range(0, $this->_phpExcel->getSheetCount() - 1);
1424
 
1425
		foreach ($sheetIndexes as $sheetIndex) {
1426
			$sheet = $this->_phpExcel->getSheet($sheetIndex);
1427
 
1428
			$candidateSpannedRow  = array();
1429
 
1430
			// loop through all Excel merged cells
1431
			foreach ($sheet->getMergeCells() as $cells) {
1432
				list($cells, ) = PHPExcel_Cell::splitRange($cells);
1433
				$first = $cells[0];
1434
				$last  = $cells[1];
1435
 
1436
				list($fc, $fr) = PHPExcel_Cell::coordinateFromString($first);
1437
				$fc = PHPExcel_Cell::columnIndexFromString($fc) - 1;
1438
 
1439
				list($lc, $lr) = PHPExcel_Cell::coordinateFromString($last);
1440
				$lc = PHPExcel_Cell::columnIndexFromString($lc) - 1;
1441
 
1442
				// loop through the individual cells in the individual merge
1443
				$r = $fr - 1;
1444
				while($r++ < $lr) {
1445
					// also, flag this row as a HTML row that is candidate to be omitted
1446
					$candidateSpannedRow[$r] = $r;
1447
 
1448
					$c = $fc - 1;
1449
					while($c++ < $lc) {
1450
						if ( !($c == $fc && $r == $fr) ) {
1451
							// not the upper-left cell (should not be written in HTML)
1452
							$this->_isSpannedCell[$sheetIndex][$r][$c] = array(
1453
								'baseCell' => array($fr, $fc),
1454
							);
1455
						} else {
1456
							// upper-left is the base cell that should hold the colspan/rowspan attribute
1457
							$this->_isBaseCell[$sheetIndex][$r][$c] = array(
1458
								'xlrowspan' => $lr - $fr + 1, // Excel rowspan
1459
								'rowspan'   => $lr - $fr + 1, // HTML rowspan, value may change
1460
								'xlcolspan' => $lc - $fc + 1, // Excel colspan
1461
								'colspan'   => $lc - $fc + 1, // HTML colspan, value may change
1462
							);
1463
						}
1464
					}
1465
				}
1466
			}
1467
 
1468
			// Identify which rows should be omitted in HTML. These are the rows where all the cells
1469
			//   participate in a merge and the where base cells are somewhere above.
1470
			$countColumns = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
1471
			foreach ($candidateSpannedRow as $rowIndex) {
1472
				if (isset($this->_isSpannedCell[$sheetIndex][$rowIndex])) {
1473
					if (count($this->_isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) {
1474
						$this->_isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex;
1475
					};
1476
				}
1477
			}
1478
 
1479
			// For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
1480
			if ( isset($this->_isSpannedRow[$sheetIndex]) ) {
1481
				foreach ($this->_isSpannedRow[$sheetIndex] as $rowIndex) {
1482
					$adjustedBaseCells = array();
1483
					$c = -1;
1484
					$e = $countColumns - 1;
1485
					while($c++ < $e) {
1486
						$baseCell = $this->_isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell'];
1487
 
1488
						if ( !in_array($baseCell, $adjustedBaseCells) ) {
1489
							// subtract rowspan by 1
1490
							--$this->_isBaseCell[$sheetIndex][ $baseCell[0] ][ $baseCell[1] ]['rowspan'];
1491
							$adjustedBaseCells[] = $baseCell;
1492
						}
1493
					}
1494
				}
1495
			}
1496
 
1497
			// TODO: Same for columns
1498
		}
1499
 
1500
		// We have calculated the spans
1501
		$this->_spansAreCalculated = true;
1502
	}
1503
 
1504
	private function _setMargins(PHPExcel_Worksheet $pSheet) {
1505
		$htmlPage = '@page { ';
1506
		$htmlBody = 'body { ';
1507
 
1508
		$left = PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getLeft()) . 'in; ';
1509
		$htmlPage .= 'left-margin: ' . $left;
1510
		$htmlBody .= 'left-margin: ' . $left;
1511
		$right = PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getRight()) . 'in; ';
1512
		$htmlPage .= 'right-margin: ' . $right;
1513
		$htmlBody .= 'right-margin: ' . $right;
1514
		$top = PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getTop()) . 'in; ';
1515
		$htmlPage .= 'top-margin: ' . $top;
1516
		$htmlBody .= 'top-margin: ' . $top;
1517
		$bottom = PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getBottom()) . 'in; ';
1518
		$htmlPage .= 'bottom-margin: ' . $bottom;
1519
		$htmlBody .= 'bottom-margin: ' . $bottom;
1520
 
1521
		$htmlPage .= "}\n";
1522
		$htmlBody .= "}\n";
1523
 
1524
		return "<style>\n" . $htmlPage . $htmlBody . "</style>\n";
1525
	}
1526
 
1527
}