5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
22 * @package PHPExcel_Writer_HTML
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
30 * PHPExcel_Writer_HTML
33 * @package PHPExcel_Writer_HTML
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 class PHPExcel_Writer_HTML
extends PHPExcel_Writer_Abstract
implements PHPExcel_Writer_IWriter
{
45 * Sheet index to write
49 private $_sheetIndex = 0;
56 private $_imagesRoot = '.';
59 * embed images, or link to images
63 private $_embedImages = FALSE;
70 private $_useInlineCss = false;
77 private $_cssStyles = null;
80 * Array of column widths in points
84 private $_columnWidths = null;
89 * @var PHPExcel_Style_Font
91 private $_defaultFont;
94 * Flag whether spans have been calculated
98 private $_spansAreCalculated = false;
101 * Excel cells that should not be written as HTML cells
105 private $_isSpannedCell = array();
108 * Excel cells that are upper-left corner in a cell merge
112 private $_isBaseCell = array();
115 * Excel rows that should not be written as HTML rows
119 private $_isSpannedRow = array();
122 * Is the current writer creating PDF?
126 protected $_isPdf = false;
129 * Generate the Navigation block
133 private $_generateSheetNavigationBlock = true;
136 * Create a new PHPExcel_Writer_HTML
138 * @param PHPExcel $phpExcel PHPExcel object
140 public function __construct(PHPExcel
$phpExcel) {
141 $this->_phpExcel
= $phpExcel;
142 $this->_defaultFont
= $this->_phpExcel
->getDefaultStyle()->getFont();
146 * Save PHPExcel to file
148 * @param string $pFilename
149 * @throws PHPExcel_Writer_Exception
151 public function save($pFilename = null) {
153 $this->_phpExcel
->garbageCollect();
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
);
161 $this->buildCSS(!$this->_useInlineCss
);
164 $fileHandle = fopen($pFilename, 'wb+');
165 if ($fileHandle === false) {
166 throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
170 fwrite($fileHandle, $this->generateHTMLHeader(!$this->_useInlineCss
));
172 // Write navigation (tabs)
173 if ((!$this->_isPdf
) && ($this->_generateSheetNavigationBlock
)) {
174 fwrite($fileHandle, $this->generateNavigation());
178 fwrite($fileHandle, $this->generateSheetData());
181 fwrite($fileHandle, $this->generateHTMLFooter());
186 PHPExcel_Calculation
::setArrayReturnType($saveArrayReturnType);
187 PHPExcel_Calculation
::getInstance($this->_phpExcel
)->getDebugLog()->setWriteDebugLog($saveDebugLog);
193 * @param string $vAlign Vertical alignment
196 private function _mapVAlign($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';
209 * @param string $hAlign Horizontal alignment
210 * @return string|false
212 private function _mapHAlign($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;
227 * @param int $borderStyle Sheet index
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
255 public function getSheetIndex() {
256 return $this->_sheetIndex
;
262 * @param int $pValue Sheet index
263 * @return PHPExcel_Writer_HTML
265 public function setSheetIndex($pValue = 0) {
266 $this->_sheetIndex
= $pValue;
275 public function getGenerateSheetNavigationBlock() {
276 return $this->_generateSheetNavigationBlock
;
282 * @param boolean $pValue Flag indicating whether the sheet navigation block should be generated or not
283 * @return PHPExcel_Writer_HTML
285 public function setGenerateSheetNavigationBlock($pValue = true) {
286 $this->_generateSheetNavigationBlock
= (bool) $pValue;
291 * Write all sheets (resets sheetIndex to NULL)
293 public function writeAllSheets() {
294 $this->_sheetIndex
= null;
299 * Generate HTML header
301 * @param boolean $pIncludeStyles Include styles?
303 * @throws PHPExcel_Writer_Exception
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.');
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
;
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
;
338 if ($pIncludeStyles) {
339 $html .= $this->generateStyles(true);
342 $html .= ' </head>' . PHP_EOL
;
343 $html .= '' . PHP_EOL
;
344 $html .= ' <body>' . PHP_EOL
;
351 * Generate sheet data
354 * @throws PHPExcel_Writer_Exception
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.');
362 // Ensure that Spans have been calculated?
363 if (!$this->_spansAreCalculated
) {
364 $this->_calculateSpans();
369 if (is_null($this->_sheetIndex
)) {
370 $sheets = $this->_phpExcel
->getAllSheets();
372 $sheets[] = $this->_phpExcel
->getSheet($this->_sheetIndex
);
380 foreach ($sheets as $sheet) {
381 // Write table header
382 $html .= $this->_generateTableHeader($sheet);
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;
392 $rowMin = $dimension[0][1];
393 $rowMax = $dimension[1][1];
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();
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;
409 // Loop through cells
411 while($row++
< $rowMax) {
413 if ($row == $theadStart) {
414 $html .= ' <thead>' . PHP_EOL
;
419 if ($row == $tbodyStart) {
420 $html .= ' <tbody>' . PHP_EOL
;
424 // Write row if there are HTML table cells in it
425 if ( !isset($this->_isSpannedRow
[$sheet->getParent()->getIndex($sheet)][$row]) ) {
426 // Start a new rowData
428 // Loop through columns
429 $column = $dimension[0][0] - 1;
430 while($column++
< $dimension[1][0]) {
432 if ($sheet->cellExistsByColumnAndRow($column, $row)) {
433 $rowData[$column] = PHPExcel_Cell
::stringFromColumnIndex($column) . $row;
435 $rowData[$column] = '';
438 $html .= $this->_generateRow($sheet, $rowData, $row - 1, $cellType);
442 if ($row == $theadEnd) {
443 $html .= ' </thead>' . PHP_EOL
;
446 $html .= $this->_extendRowsForChartsAndImages($sheet, $row);
449 $html .= ' </tbody>' . PHP_EOL
;
451 // Write table footer
452 $html .= $this->_generateTableFooter();
456 if (is_null($this->_sheetIndex
) && $sheetId +
1 < $this->_phpExcel
->getSheetCount()) {
457 $html .= '<div style="page-break-before:always" />';
470 * Generate sheet tabs
473 * @throws PHPExcel_Writer_Exception
475 public function generateNavigation()
477 // PHPExcel object known?
478 if (is_null($this->_phpExcel
)) {
479 throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
484 if (is_null($this->_sheetIndex
)) {
485 $sheets = $this->_phpExcel
->getAllSheets();
487 $sheets[] = $this->_phpExcel
->getSheet($this->_sheetIndex
);
493 // Only if there are more than 1 sheets
494 if (count($sheets) > 1) {
498 $html .= '<ul class="navigation">' . PHP_EOL
;
500 foreach ($sheets as $sheet) {
501 $html .= ' <li class="sheet' . $sheetId . '"><a href="#sheet' . $sheetId . '">' . $sheet->getTitle() . '</a></li>' . PHP_EOL
;
505 $html .= '</ul>' . PHP_EOL
;
511 private function _extendRowsForChartsAndImages(PHPExcel_Worksheet
$pSheet, $row) {
514 if ($this->_includeCharts
) {
515 foreach ($pSheet->getChartCollection() as $chart) {
516 if ($chart instanceof PHPExcel_Chart
) {
517 $chartCoordinates = $chart->getTopLeftPosition();
518 $chartTL = PHPExcel_Cell
::coordinateFromString($chartCoordinates['cell']);
519 $chartCol = PHPExcel_Cell
::columnIndexFromString($chartTL[0]);
520 if ($chartTL[1] > $rowMax) {
521 $rowMax = $chartTL[1];
522 if ($chartCol > PHPExcel_Cell
::columnIndexFromString($colMax)) {
523 $colMax = $chartTL[0];
530 foreach ($pSheet->getDrawingCollection() as $drawing) {
531 if ($drawing instanceof PHPExcel_Worksheet_Drawing
) {
532 $imageTL = PHPExcel_Cell
::coordinateFromString($drawing->getCoordinates());
533 $imageCol = PHPExcel_Cell
::columnIndexFromString($imageTL[0]);
534 if ($imageTL[1] > $rowMax) {
535 $rowMax = $imageTL[1];
536 if ($imageCol > PHPExcel_Cell
::columnIndexFromString($colMax)) {
537 $colMax = $imageTL[0];
544 while ($row < $rowMax) {
546 for ($col = 'A'; $col != $colMax; ++
$col) {
548 $html .= $this->_writeImageInCell($pSheet, $col.$row);
549 if ($this->_includeCharts
) {
550 $html .= $this->_writeChartInCell($pSheet, $col.$row);
562 * Generate image tag in cell
564 * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
565 * @param string $coordinates Cell coordinates
567 * @throws PHPExcel_Writer_Exception
569 private function _writeImageInCell(PHPExcel_Worksheet
$pSheet, $coordinates) {
574 foreach ($pSheet->getDrawingCollection() as $drawing) {
575 if ($drawing instanceof PHPExcel_Worksheet_Drawing
) {
576 if ($drawing->getCoordinates() == $coordinates) {
577 $filename = $drawing->getPath();
579 // Strip off eventual '.'
580 if (substr($filename, 0, 1) == '.') {
581 $filename = substr($filename, 1);
584 // Prepend images root
585 $filename = $this->getImagesRoot() . $filename;
587 // Strip off eventual '.'
588 if (substr($filename, 0, 1) == '.' && substr($filename, 0, 2) != './') {
589 $filename = substr($filename, 1);
592 // Convert UTF8 data to PCDATA
593 $filename = htmlspecialchars($filename);
596 if ((!$this->_embedImages
) ||
($this->_isPdf
)) {
597 $imageData = $filename;
599 $imageDetails = getimagesize($filename);
600 if ($fp = fopen($filename,"rb", 0)) {
601 $picture = fread($fp,filesize($filename));
603 // base64 encode the binary data, then break it
604 // into chunks according to RFC 2045 semantics
605 $base64 = chunk_split(base64_encode($picture));
606 $imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64;
608 $imageData = $filename;
612 $html .= '<div style="position: relative;">';
613 $html .= '<img style="position: absolute; z-index: 1; left: ' .
614 $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' .
615 $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' .
616 $imageData . '" border="0" />';
627 * Generate chart tag in cell
629 * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
630 * @param string $coordinates Cell coordinates
632 * @throws PHPExcel_Writer_Exception
634 private function _writeChartInCell(PHPExcel_Worksheet
$pSheet, $coordinates) {
639 foreach ($pSheet->getChartCollection() as $chart) {
640 if ($chart instanceof PHPExcel_Chart
) {
641 $chartCoordinates = $chart->getTopLeftPosition();
642 if ($chartCoordinates['cell'] == $coordinates) {
643 $chartFileName = PHPExcel_Shared_File
::sys_get_temp_dir().'/'.uniqid().'.png';
644 if (!$chart->render($chartFileName)) {
649 $imageDetails = getimagesize($chartFileName);
650 if ($fp = fopen($chartFileName,"rb", 0)) {
651 $picture = fread($fp,filesize($chartFileName));
653 // base64 encode the binary data, then break it
654 // into chunks according to RFC 2045 semantics
655 $base64 = chunk_split(base64_encode($picture));
656 $imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64;
658 $html .= '<div style="position: relative;">';
659 $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
;
662 unlink($chartFileName);
673 * Generate CSS styles
675 * @param boolean $generateSurroundingHTML Generate surrounding HTML tags? (<style> and </style>)
677 * @throws PHPExcel_Writer_Exception
679 public function generateStyles($generateSurroundingHTML = true) {
680 // PHPExcel object known?
681 if (is_null($this->_phpExcel
)) {
682 throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
686 $css = $this->buildCSS($generateSurroundingHTML);
692 if ($generateSurroundingHTML) {
693 $html .= ' <style type="text/css">' . PHP_EOL
;
694 $html .= ' html { ' . $this->_assembleCSS($css['html']) . ' }' . PHP_EOL
;
697 // Write all other styles
698 foreach ($css as $styleName => $styleDefinition) {
699 if ($styleName != 'html') {
700 $html .= ' ' . $styleName . ' { ' . $this->_assembleCSS($styleDefinition) . ' }' . PHP_EOL
;
705 if ($generateSurroundingHTML) {
706 $html .= ' </style>' . PHP_EOL
;
716 * @param boolean $generateSurroundingHTML Generate surrounding HTML style? (html { })
718 * @throws PHPExcel_Writer_Exception
720 public function buildCSS($generateSurroundingHTML = true) {
721 // PHPExcel object known?
722 if (is_null($this->_phpExcel
)) {
723 throw new PHPExcel_Writer_Exception('Internal PHPExcel object not set to an instance of an object.');
727 if (!is_null($this->_cssStyles
)) {
728 return $this->_cssStyles
;
731 // Ensure that spans have been calculated
732 if (!$this->_spansAreCalculated
) {
733 $this->_calculateSpans();
740 if ($generateSurroundingHTML) {
742 $css['html']['font-family'] = 'Calibri, Arial, Helvetica, sans-serif';
743 $css['html']['font-size'] = '11pt';
744 $css['html']['background-color'] = 'white';
749 $css['table']['border-collapse'] = 'collapse';
750 if (!$this->_isPdf
) {
751 $css['table']['page-break-after'] = 'always';
755 $css['.gridlines td']['border'] = '1px dotted black';
756 $css['.gridlines th']['border'] = '1px dotted black';
759 $css['.b']['text-align'] = 'center'; // BOOL
762 $css['.e']['text-align'] = 'center'; // ERROR
765 $css['.f']['text-align'] = 'right'; // FORMULA
768 $css['.inlineStr']['text-align'] = 'left'; // INLINE
771 $css['.n']['text-align'] = 'right'; // NUMERIC
774 $css['.s']['text-align'] = 'left'; // STRING
776 // Calculate cell style hashes
777 foreach ($this->_phpExcel
->getCellXfCollection() as $index => $style) {
778 $css['td.style' . $index] = $this->_createCSSStyle( $style );
779 $css['th.style' . $index] = $this->_createCSSStyle( $style );
784 if (is_null($this->_sheetIndex
)) {
785 $sheets = $this->_phpExcel
->getAllSheets();
787 $sheets[] = $this->_phpExcel
->getSheet($this->_sheetIndex
);
790 // Build styles per sheet
791 foreach ($sheets as $sheet) {
792 // Calculate hash code
793 $sheetIndex = $sheet->getParent()->getIndex($sheet);
796 // Calculate column widths
797 $sheet->calculateColumnWidths();
799 // col elements, initialize
800 $highestColumnIndex = PHPExcel_Cell
::columnIndexFromString($sheet->getHighestColumn()) - 1;
802 while($column++
< $highestColumnIndex) {
803 $this->_columnWidths
[$sheetIndex][$column] = 42; // approximation
804 $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = '42pt';
807 // col elements, loop through columnDimensions and set width
808 foreach ($sheet->getColumnDimensions() as $columnDimension) {
809 if (($width = PHPExcel_Shared_Drawing
::cellDimensionToPixels($columnDimension->getWidth(), $this->_defaultFont
)) >= 0) {
810 $width = PHPExcel_Shared_Drawing
::pixelsToPoints($width);
811 $column = PHPExcel_Cell
::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
812 $this->_columnWidths
[$sheetIndex][$column] = $width;
813 $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt';
815 if ($columnDimension->getVisible() === false) {
816 $css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse';
817 $css['table.sheet' . $sheetIndex . ' col.col' . $column]['*display'] = 'none'; // target IE6+7
822 // Default row height
823 $rowDimension = $sheet->getDefaultRowDimension();
825 // table.sheetN tr { }
826 $css['table.sheet' . $sheetIndex . ' tr'] = array();
828 if ($rowDimension->getRowHeight() == -1) {
829 $pt_height = PHPExcel_Shared_Font
::getDefaultRowHeightByFont($this->_phpExcel
->getDefaultStyle()->getFont());
831 $pt_height = $rowDimension->getRowHeight();
833 $css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt';
834 if ($rowDimension->getVisible() === false) {
835 $css['table.sheet' . $sheetIndex . ' tr']['display'] = 'none';
836 $css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden';
839 // Calculate row heights
840 foreach ($sheet->getRowDimensions() as $rowDimension) {
841 $row = $rowDimension->getRowIndex() - 1;
843 // table.sheetN tr.rowYYYYYY { }
844 $css['table.sheet' . $sheetIndex . ' tr.row' . $row] = array();
846 if ($rowDimension->getRowHeight() == -1) {
847 $pt_height = PHPExcel_Shared_Font
::getDefaultRowHeightByFont($this->_phpExcel
->getDefaultStyle()->getFont());
849 $pt_height = $rowDimension->getRowHeight();
851 $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt';
852 if ($rowDimension->getVisible() === false) {
853 $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none';
854 $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden';
860 if (is_null($this->_cssStyles
)) {
861 $this->_cssStyles
= $css;
871 * @param PHPExcel_Style $pStyle PHPExcel_Style
874 private function _createCSSStyle(PHPExcel_Style
$pStyle) {
880 $this->_createCSSStyleAlignment($pStyle->getAlignment())
881 , $this->_createCSSStyleBorders($pStyle->getBorders())
882 , $this->_createCSSStyleFont($pStyle->getFont())
883 , $this->_createCSSStyleFill($pStyle->getFill())
891 * Create CSS style (PHPExcel_Style_Alignment)
893 * @param PHPExcel_Style_Alignment $pStyle PHPExcel_Style_Alignment
896 private function _createCSSStyleAlignment(PHPExcel_Style_Alignment
$pStyle) {
901 $css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
902 if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) {
903 $css['text-align'] = $textAlign;
904 if(in_array($textAlign,array('left','right')))
905 $css['padding-'.$textAlign] = (string)((int)$pStyle->getIndent() * 9).'px';
913 * Create CSS style (PHPExcel_Style_Font)
915 * @param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
918 private function _createCSSStyleFont(PHPExcel_Style_Font
$pStyle) {
923 if ($pStyle->getBold()) {
924 $css['font-weight'] = 'bold';
926 if ($pStyle->getUnderline() != PHPExcel_Style_Font
::UNDERLINE_NONE
&& $pStyle->getStrikethrough()) {
927 $css['text-decoration'] = 'underline line-through';
928 } else if ($pStyle->getUnderline() != PHPExcel_Style_Font
::UNDERLINE_NONE
) {
929 $css['text-decoration'] = 'underline';
930 } else if ($pStyle->getStrikethrough()) {
931 $css['text-decoration'] = 'line-through';
933 if ($pStyle->getItalic()) {
934 $css['font-style'] = 'italic';
937 $css['color'] = '#' . $pStyle->getColor()->getRGB();
938 $css['font-family'] = '\'' . $pStyle->getName() . '\'';
939 $css['font-size'] = $pStyle->getSize() . 'pt';
946 * Create CSS style (PHPExcel_Style_Borders)
948 * @param PHPExcel_Style_Borders $pStyle PHPExcel_Style_Borders
951 private function _createCSSStyleBorders(PHPExcel_Style_Borders
$pStyle) {
956 $css['border-bottom'] = $this->_createCSSStyleBorder($pStyle->getBottom());
957 $css['border-top'] = $this->_createCSSStyleBorder($pStyle->getTop());
958 $css['border-left'] = $this->_createCSSStyleBorder($pStyle->getLeft());
959 $css['border-right'] = $this->_createCSSStyleBorder($pStyle->getRight());
966 * Create CSS style (PHPExcel_Style_Border)
968 * @param PHPExcel_Style_Border $pStyle PHPExcel_Style_Border
971 private function _createCSSStyleBorder(PHPExcel_Style_Border
$pStyle) {
973 // $css = $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
974 // Create CSS - add !important to non-none border styles for merged cells
975 $borderStyle = $this->_mapBorderStyle($pStyle->getBorderStyle());
976 $css = $borderStyle . ' #' . $pStyle->getColor()->getRGB() . (($borderStyle == 'none') ?
'' : ' !important');
983 * Create CSS style (PHPExcel_Style_Fill)
985 * @param PHPExcel_Style_Fill $pStyle PHPExcel_Style_Fill
988 private function _createCSSStyleFill(PHPExcel_Style_Fill
$pStyle) {
993 $value = $pStyle->getFillType() == PHPExcel_Style_Fill
::FILL_NONE ?
994 'white' : '#' . $pStyle->getStartColor()->getRGB();
995 $css['background-color'] = $value;
1002 * Generate HTML footer
1004 public function generateHTMLFooter() {
1007 $html .= ' </body>' . PHP_EOL
;
1008 $html .= '</html>' . PHP_EOL
;
1015 * Generate table header
1017 * @param PHPExcel_Worksheet $pSheet The worksheet for the table we are writing
1019 * @throws PHPExcel_Writer_Exception
1021 private function _generateTableHeader($pSheet) {
1022 $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
1026 $html .= $this->_setMargins($pSheet);
1028 if (!$this->_useInlineCss
) {
1029 $gridlines = $pSheet->getShowGridlines() ?
' gridlines' : '';
1030 $html .= ' <table border="0" cellpadding="0" cellspacing="0" id="sheet' . $sheetIndex . '" class="sheet' . $sheetIndex . $gridlines . '">' . PHP_EOL
;
1032 $style = isset($this->_cssStyles
['table']) ?
1033 $this->_assembleCSS($this->_cssStyles
['table']) : '';
1035 if ($this->_isPdf
&& $pSheet->getShowGridlines()) {
1036 $html .= ' <table border="1" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="1" style="' . $style . '">' . PHP_EOL
;
1038 $html .= ' <table border="0" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="0" style="' . $style . '">' . PHP_EOL
;
1042 // Write <col> elements
1043 $highestColumnIndex = PHPExcel_Cell
::columnIndexFromString($pSheet->getHighestColumn()) - 1;
1045 while($i++
< $highestColumnIndex) {
1046 if (!$this->_isPdf
) {
1047 if (!$this->_useInlineCss
) {
1048 $html .= ' <col class="col' . $i . '">' . PHP_EOL
;
1050 $style = isset($this->_cssStyles
['table.sheet' . $sheetIndex . ' col.col' . $i]) ?
1051 $this->_assembleCSS($this->_cssStyles
['table.sheet' . $sheetIndex . ' col.col' . $i]) : '';
1052 $html .= ' <col style="' . $style . '">' . PHP_EOL
;
1062 * Generate table footer
1064 * @throws PHPExcel_Writer_Exception
1066 private function _generateTableFooter() {
1069 $html .= ' </table>' . PHP_EOL
;
1078 * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
1079 * @param array $pValues Array containing cells in a row
1080 * @param int $pRow Row number (0-based)
1082 * @throws PHPExcel_Writer_Exception
1084 private function _generateRow(PHPExcel_Worksheet
$pSheet, $pValues = null, $pRow = 0, $cellType = 'td') {
1085 if (is_array($pValues)) {
1090 $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
1092 // DomPDF and breaks
1093 if ($this->_isPdf
&& count($pSheet->getBreaks()) > 0) {
1094 $breaks = $pSheet->getBreaks();
1096 // check if a break is needed before this row
1097 if (isset($breaks['A' . $pRow])) {
1098 // close table: </table>
1099 $html .= $this->_generateTableFooter();
1101 // insert page break
1102 $html .= '<div style="page-break-before:always" />';
1104 // open table again: <table> + <col> etc.
1105 $html .= $this->_generateTableHeader($pSheet);
1110 if (!$this->_useInlineCss
) {
1111 $html .= ' <tr class="row' . $pRow . '">' . PHP_EOL
;
1113 $style = isset($this->_cssStyles
['table.sheet' . $sheetIndex . ' tr.row' . $pRow])
1114 ?
$this->_assembleCSS($this->_cssStyles
['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
1116 $html .= ' <tr style="' . $style . '">' . PHP_EOL
;
1121 foreach ($pValues as $cellAddress) {
1122 $cell = ($cellAddress > '') ?
$pSheet->getCell($cellAddress) : '';
1123 $coordinate = PHPExcel_Cell
::stringFromColumnIndex($colNum) . ($pRow +
1);
1124 if (!$this->_useInlineCss
) {
1126 $cssClass = 'column' . $colNum;
1128 $cssClass = array();
1129 if ($cellType == 'th') {
1130 if (isset($this->_cssStyles
['table.sheet' . $sheetIndex . ' th.column' . $colNum])) {
1131 $this->_cssStyles
['table.sheet' . $sheetIndex . ' th.column' . $colNum];
1134 if (isset($this->_cssStyles
['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
1135 $this->_cssStyles
['table.sheet' . $sheetIndex . ' td.column' . $colNum];
1143 $cellData = ' ';
1146 if ($cell instanceof PHPExcel_Cell
) {
1148 if (is_null($cell->getParent())) {
1149 $cell->attach($pSheet);
1152 if ($cell->getValue() instanceof PHPExcel_RichText
) {
1153 // Loop through rich text elements
1154 $elements = $cell->getValue()->getRichTextElements();
1155 foreach ($elements as $element) {
1157 if ($element instanceof PHPExcel_RichText_Run
) {
1158 $cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';
1160 if ($element->getFont()->getSuperScript()) {
1161 $cellData .= '<sup>';
1162 } else if ($element->getFont()->getSubScript()) {
1163 $cellData .= '<sub>';
1167 // Convert UTF8 data to PCDATA
1168 $cellText = $element->getText();
1169 $cellData .= htmlspecialchars($cellText);
1171 if ($element instanceof PHPExcel_RichText_Run
) {
1172 if ($element->getFont()->getSuperScript()) {
1173 $cellData .= '</sup>';
1174 } else if ($element->getFont()->getSubScript()) {
1175 $cellData .= '</sub>';
1178 $cellData .= '</span>';
1182 if ($this->_preCalculateFormulas
) {
1183 $cellData = PHPExcel_Style_NumberFormat
::toFormattedString(
1184 $cell->getCalculatedValue(),
1185 $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
1186 array($this, 'formatColor')
1189 $cellData = PHPExcel_Style_NumberFormat
::toFormattedString(
1191 $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
1192 array($this, 'formatColor')
1195 $cellData = htmlspecialchars($cellData);
1196 if ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSuperScript()) {
1197 $cellData = '<sup>'.$cellData.'</sup>';
1198 } elseif ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSubScript()) {
1199 $cellData = '<sub>'.$cellData.'</sub>';
1203 // Converts the cell content so that spaces occuring at beginning of each new line are replaced by
1204 // Example: " Hello\n to the world" is converted to " Hello\n to the world"
1205 $cellData = preg_replace("/(?m)(?:^|\\G) /", ' ', $cellData);
1207 // convert newline "\n" to '<br>'
1208 $cellData = nl2br($cellData);
1210 // Extend CSS class?
1211 if (!$this->_useInlineCss
) {
1212 $cssClass .= ' style' . $cell->getXfIndex();
1213 $cssClass .= ' ' . $cell->getDataType();
1215 if ($cellType == 'th') {
1216 if (isset($this->_cssStyles
['th.style' . $cell->getXfIndex()])) {
1217 $cssClass = array_merge($cssClass, $this->_cssStyles
['th.style' . $cell->getXfIndex()]);
1220 if (isset($this->_cssStyles
['td.style' . $cell->getXfIndex()])) {
1221 $cssClass = array_merge($cssClass, $this->_cssStyles
['td.style' . $cell->getXfIndex()]);
1225 // General horizontal alignment: Actual horizontal alignment depends on dataType
1226 $sharedStyle = $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() );
1227 if ($sharedStyle->getAlignment()->getHorizontal() == PHPExcel_Style_Alignment
::HORIZONTAL_GENERAL
1228 && isset($this->_cssStyles
['.' . $cell->getDataType()]['text-align']))
1230 $cssClass['text-align'] = $this->_cssStyles
['.' . $cell->getDataType()]['text-align'];
1236 if ($pSheet->hyperlinkExists($coordinate) && !$pSheet->getHyperlink($coordinate)->isInternal()) {
1237 $cellData = '<a href="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getUrl()) . '" title="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getTooltip()) . '">' . $cellData . '</a>';
1240 // Should the cell be written or is it swallowed by a rowspan or colspan?
1241 $writeCell = ! ( isset($this->_isSpannedCell
[$pSheet->getParent()->getIndex($pSheet)][$pRow +
1][$colNum])
1242 && $this->_isSpannedCell
[$pSheet->getParent()->getIndex($pSheet)][$pRow +
1][$colNum] );
1244 // Colspan and Rowspan
1247 if (isset($this->_isBaseCell
[$pSheet->getParent()->getIndex($pSheet)][$pRow +
1][$colNum])) {
1248 $spans = $this->_isBaseCell
[$pSheet->getParent()->getIndex($pSheet)][$pRow +
1][$colNum];
1249 $rowSpan = $spans['rowspan'];
1250 $colSpan = $spans['colspan'];
1252 // Also apply style from last cell in merge to fix borders -
1253 // relies on !important for non-none border declarations in _createCSSStyleBorder
1254 $endCellCoord = PHPExcel_Cell
::stringFromColumnIndex($colNum +
$colSpan - 1) . ($pRow +
$rowSpan);
1255 if (!$this->_useInlineCss
) {
1256 $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
1263 $html .= ' <' . $cellType;
1264 if (!$this->_useInlineCss
) {
1265 $html .= ' class="' . $cssClass . '"';
1267 //** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
1268 // We must explicitly write the width of the <td> element because TCPDF
1269 // does not recognize e.g. <col style="width:42pt">
1272 $e = $colNum +
$colSpan - 1;
1274 if (isset($this->_columnWidths
[$sheetIndex][$i])) {
1275 $width +
= $this->_columnWidths
[$sheetIndex][$i];
1278 $cssClass['width'] = $width . 'pt';
1280 // We must also explicitly write the height of the <td> element because TCPDF
1281 // does not recognize e.g. <tr style="height:50pt">
1282 if (isset($this->_cssStyles
['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
1283 $height = $this->_cssStyles
['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
1284 $cssClass['height'] = $height;
1286 //** end of redundant code **
1288 $html .= ' style="' . $this->_assembleCSS($cssClass) . '"';
1291 $html .= ' colspan="' . $colSpan . '"';
1294 $html .= ' rowspan="' . $rowSpan . '"';
1299 $html .= $this->_writeImageInCell($pSheet, $coordinate);
1302 if ($this->_includeCharts
) {
1303 $html .= $this->_writeChartInCell($pSheet, $coordinate);
1310 $html .= '</'.$cellType.'>' . PHP_EOL
;
1318 $html .= ' </tr>' . PHP_EOL
;
1323 throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
1328 * Takes array where of CSS properties / values and converts to CSS string
1333 private function _assembleCSS($pValue = array())
1336 foreach ($pValue as $property => $value) {
1337 $pairs[] = $property . ':' . $value;
1339 $string = implode('; ', $pairs);
1349 public function getImagesRoot() {
1350 return $this->_imagesRoot
;
1356 * @param string $pValue
1357 * @return PHPExcel_Writer_HTML
1359 public function setImagesRoot($pValue = '.') {
1360 $this->_imagesRoot
= $pValue;
1369 public function getEmbedImages() {
1370 return $this->_embedImages
;
1376 * @param boolean $pValue
1377 * @return PHPExcel_Writer_HTML
1379 public function setEmbedImages($pValue = '.') {
1380 $this->_embedImages
= $pValue;
1385 * Get use inline CSS?
1389 public function getUseInlineCss() {
1390 return $this->_useInlineCss
;
1394 * Set use inline CSS?
1396 * @param boolean $pValue
1397 * @return PHPExcel_Writer_HTML
1399 public function setUseInlineCss($pValue = false) {
1400 $this->_useInlineCss
= $pValue;
1405 * Add color to formatted string as inline style
1407 * @param string $pValue Plain formatted value without color
1408 * @param string $pFormat Format code
1411 public function formatColor($pValue, $pFormat)
1413 // Color information, e.g. [Red] is always at the beginning
1414 $color = null; // initialize
1417 $color_regex = '/^\\[[a-zA-Z]+\\]/';
1418 if (preg_match($color_regex, $pFormat, $matches)) {
1419 $color = str_replace('[', '', $matches[0]);
1420 $color = str_replace(']', '', $color);
1421 $color = strtolower($color);
1424 // convert to PCDATA
1425 $value = htmlspecialchars($pValue);
1428 if ($color !== null) {
1429 $value = '<span style="color:' . $color . '">' . $value . '</span>';
1436 * Calculate information about HTML colspan and rowspan which is not always the same as Excel's
1438 private function _calculateSpans()
1440 // Identify all cells that should be omitted in HTML due to cell merge.
1441 // In HTML only the upper-left cell should be written and it should have
1442 // appropriate rowspan / colspan attribute
1443 $sheetIndexes = $this->_sheetIndex
!== null ?
1444 array($this->_sheetIndex
) : range(0, $this->_phpExcel
->getSheetCount() - 1);
1446 foreach ($sheetIndexes as $sheetIndex) {
1447 $sheet = $this->_phpExcel
->getSheet($sheetIndex);
1449 $candidateSpannedRow = array();
1451 // loop through all Excel merged cells
1452 foreach ($sheet->getMergeCells() as $cells) {
1453 list($cells, ) = PHPExcel_Cell
::splitRange($cells);
1457 list($fc, $fr) = PHPExcel_Cell
::coordinateFromString($first);
1458 $fc = PHPExcel_Cell
::columnIndexFromString($fc) - 1;
1460 list($lc, $lr) = PHPExcel_Cell
::coordinateFromString($last);
1461 $lc = PHPExcel_Cell
::columnIndexFromString($lc) - 1;
1463 // loop through the individual cells in the individual merge
1466 // also, flag this row as a HTML row that is candidate to be omitted
1467 $candidateSpannedRow[$r] = $r;
1471 if ( !($c == $fc && $r == $fr) ) {
1472 // not the upper-left cell (should not be written in HTML)
1473 $this->_isSpannedCell
[$sheetIndex][$r][$c] = array(
1474 'baseCell' => array($fr, $fc),
1477 // upper-left is the base cell that should hold the colspan/rowspan attribute
1478 $this->_isBaseCell
[$sheetIndex][$r][$c] = array(
1479 'xlrowspan' => $lr - $fr +
1, // Excel rowspan
1480 'rowspan' => $lr - $fr +
1, // HTML rowspan, value may change
1481 'xlcolspan' => $lc - $fc +
1, // Excel colspan
1482 'colspan' => $lc - $fc +
1, // HTML colspan, value may change
1489 // Identify which rows should be omitted in HTML. These are the rows where all the cells
1490 // participate in a merge and the where base cells are somewhere above.
1491 $countColumns = PHPExcel_Cell
::columnIndexFromString($sheet->getHighestColumn());
1492 foreach ($candidateSpannedRow as $rowIndex) {
1493 if (isset($this->_isSpannedCell
[$sheetIndex][$rowIndex])) {
1494 if (count($this->_isSpannedCell
[$sheetIndex][$rowIndex]) == $countColumns) {
1495 $this->_isSpannedRow
[$sheetIndex][$rowIndex] = $rowIndex;
1500 // For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
1501 if ( isset($this->_isSpannedRow
[$sheetIndex]) ) {
1502 foreach ($this->_isSpannedRow
[$sheetIndex] as $rowIndex) {
1503 $adjustedBaseCells = array();
1505 $e = $countColumns - 1;
1507 $baseCell = $this->_isSpannedCell
[$sheetIndex][$rowIndex][$c]['baseCell'];
1509 if ( !in_array($baseCell, $adjustedBaseCells) ) {
1510 // subtract rowspan by 1
1511 --$this->_isBaseCell
[$sheetIndex][ $baseCell[0] ][ $baseCell[1] ]['rowspan'];
1512 $adjustedBaseCells[] = $baseCell;
1518 // TODO: Same for columns
1521 // We have calculated the spans
1522 $this->_spansAreCalculated
= true;
1525 private function _setMargins(PHPExcel_Worksheet
$pSheet) {
1526 $htmlPage = '@page { ';
1527 $htmlBody = 'body { ';
1529 $left = PHPExcel_Shared_String
::FormatNumber($pSheet->getPageMargins()->getLeft()) . 'in; ';
1530 $htmlPage .= 'left-margin: ' . $left;
1531 $htmlBody .= 'left-margin: ' . $left;
1532 $right = PHPExcel_Shared_String
::FormatNumber($pSheet->getPageMargins()->getRight()) . 'in; ';
1533 $htmlPage .= 'right-margin: ' . $right;
1534 $htmlBody .= 'right-margin: ' . $right;
1535 $top = PHPExcel_Shared_String
::FormatNumber($pSheet->getPageMargins()->getTop()) . 'in; ';
1536 $htmlPage .= 'top-margin: ' . $top;
1537 $htmlBody .= 'top-margin: ' . $top;
1538 $bottom = PHPExcel_Shared_String
::FormatNumber($pSheet->getPageMargins()->getBottom()) . 'in; ';
1539 $htmlPage .= 'bottom-margin: ' . $bottom;
1540 $htmlBody .= 'bottom-margin: ' . $bottom;
1545 return "<style>\n" . $htmlPage . $htmlBody . "</style>\n";