Source for file HTML.php
Documentation is available at HTML.php
* Copyright (c) 2006 - 2009 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.0, 2009-08-10
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
require_once PHPEXCEL_ROOT . 'PHPExcel/RichText.php';
/** PHPExcel_Shared_Drawing */
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Drawing.php';
/** PHPExcel_Shared_String */
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
/** PHPExcel_HashTable */
require_once PHPEXCEL_ROOT . 'PHPExcel/HashTable.php';
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* Array of column widths in points
* Is the current writer creating PDF?
* Create a new PHPExcel_Writer_HTML
* @param PHPExcel $phpExcel PHPExcel object
* @param string $pFileName
public function save($pFilename = null) {
$fileHandle = fopen($pFilename, 'w');
if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for writing.");
default: return 'baseline';
default: return '1px solid'; // map others to thin
* @param int $pValue Sheet index
* @return PHPExcel_Writer_HTML
* Write all sheets (resets sheetIndex to NULL)
* @param boolean $pIncludeStyles Include styles?
// PHPExcel object known?
throw new Exception('Internal PHPExcel object not set to an instance of an object.');
$html .= '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' . "\r\n";
$html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' . "\r\n";
$html .= '<html>' . "\r\n";
$html .= ' <head>' . "\r\n";
$html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . "\r\n";
$html .= ' </head>' . "\r\n";
$html .= ' <body>' . "\r\n";
// PHPExcel object known?
throw new Exception('Internal PHPExcel object not set to an instance of an object.');
foreach ($sheets as $sheet) {
$cellCollection = $sheet->getCellCollection();
// Get worksheet dimension
$dimension = explode(':', $sheet->calculateWorksheetDimension());
for ($row = $dimension[0][1]; $row <= $dimension[1][1]; ++ $row) {
for ($column = $dimension[0][0]; $column <= $dimension[1][0]; ++ $column) {
if ($sheet->cellExistsByColumnAndRow($column, $row)) {
$rowData[$column] = $sheet->getCellByColumnAndRow($column, $row);
$html .= '<tcpdf method="AddPage" />';
* Generate image tag in cell
* @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
* @param string $coordinates Cell coordinates
foreach ($pSheet->getDrawingCollection() as $drawing) {
if ($drawing->getCoordinates() == $coordinates) {
$filename = $drawing->getPath();
// Strip off eventual '.'
if (substr($filename, 0, 1) == '.') {
$filename = substr($filename, 1);
// Strip off eventual '.'
if (substr($filename, 0, 1) == '.' && substr($filename, 0, 2) != './') {
$filename = substr($filename, 1);
// Convert UTF8 data to PCDATA
$html .= ' <img style="position: relative; left: ' . $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' . $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' . $filename . '" border="0" width="' . $drawing->getWidth() . '" height="' . $drawing->getHeight() . '" />' . "\r\n";
* @param boolean $generateSurroundingHTML Generate surrounding HTML tags? (<style> and </style>)
// PHPExcel object known?
throw new Exception('Internal PHPExcel object not set to an instance of an object.');
$css = $this->buildCSS($generateSurroundingHTML);
if ($generateSurroundingHTML) {
$html .= ' <style type="text/css">' . "\r\n";
$html .= ' html { ' . $this->_assembleCSS($css['html']) . ' }' . "\r\n";
// Write all other styles
foreach ($css as $styleName => $styleDefinition) {
if ($styleName != 'html') {
$html .= ' ' . $styleName . ' { ' . $this->_assembleCSS($styleDefinition) . ' }' . "\r\n";
if ($generateSurroundingHTML) {
$html .= ' </style>' . "\r\n";
* @param boolean $generateSurroundingHTML Generate surrounding HTML style? (html { })
public function buildCSS($generateSurroundingHTML = true) {
// PHPExcel object known?
throw new Exception('Internal PHPExcel object not set to an instance of an object.');
if ($generateSurroundingHTML) {
$css['html']['font-family'] = 'Calibri, Arial, Helvetica, sans-serif';
$css['html']['font-size'] = '11pt';
$css['html']['background-color'] = 'white';
$css['table']['border-collapse'] = 'collapse';
$css['table']['page-break-after'] = 'always';
$css['.gridlines td']['border'] = '1px dotted black';
$css['.b']['text-align'] = 'center'; // BOOL
$css['.e']['text-align'] = 'center'; // ERROR
$css['.f']['text-align'] = 'right'; // FORMULA
$css['.inlineStr']['text-align'] = 'left'; // INLINE
$css['.n']['text-align'] = 'right'; // NUMERIC
$css['.s']['text-align'] = 'left'; // STRING
// Calculate cell style hashes
foreach ($this->_phpExcel->getCellXfCollection() as $index => $style) {
// Build styles per sheet
foreach ($sheets as $sheet) {
$sheetIndex = $sheet->getParent()->getIndex($sheet);
// Calculate column widths
$sheet->calculateColumnWidths();
// col elements, initialize
for ($column = 0; $column <= $highestColumnIndex; ++ $column) {
$this->_columnWidths[$sheetIndex][$column] = 42; // approximation
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = '42pt';
// col elements, loop through columnDimensions and set width
foreach ($sheet->getColumnDimensions() as $columnDimension) {
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt';
if ($columnDimension->getVisible() === false) {
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse';
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['*display'] = 'none'; // target IE6+7
$rowDimension = $sheet->getDefaultRowDimension();
$css['table.sheet' . $sheetIndex . ' tr'] = array();
$pt_height = $rowDimension->getRowHeight();
$css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt';
if ($rowDimension->getVisible() === false) {
$css['table.sheet' . $sheetIndex . ' tr']['display'] = 'none';
$css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden';
foreach ($sheet->getRowDimensions() as $rowDimension) {
$row = $rowDimension->getRowIndex() - 1;
// table.sheetN tr.rowYYYYYY { }
$css['table.sheet' . $sheetIndex . ' tr.row' . $row] = array();
$pt_height = $rowDimension->getRowHeight();
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt';
if ($rowDimension->getVisible() === false) {
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none';
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden';
* @param PHPExcel_Style $pStyle PHPExcel_Style
* Create CSS style (PHPExcel_Style_Alignment)
* @param PHPExcel_Style_Alignment $pStyle PHPExcel_Style_Alignment
$css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) {
$css['text-align'] = $textAlign;
* Create CSS style (PHPExcel_Style_Font)
* @param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
if ($pStyle->getBold()) {
$css['font-weight'] = 'bold';
if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
$css['text-decoration'] = 'underline line-through';
$css['text-decoration'] = 'underline';
} else if ($pStyle->getStrikethrough()) {
$css['text-decoration'] = 'line-through';
if ($pStyle->getItalic()) {
$css['font-style'] = 'italic';
$css['color'] = '#' . $pStyle->getColor()->getRGB();
$css['font-family'] = '\'' . $pStyle->getName() . '\'';
$css['font-size'] = $pStyle->getSize() . 'pt';
* Create CSS style (PHPExcel_Style_Borders)
* @param PHPExcel_Style_Borders $pStyle PHPExcel_Style_Borders
* Create CSS style (PHPExcel_Style_Border)
* @param PHPExcel_Style_Border $pStyle PHPExcel_Style_Border
$css .= $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
* Create CSS style (PHPExcel_Style_Fill)
* @param PHPExcel_Style_Fill $pStyle PHPExcel_Style_Fill
'white' : '#' . $pStyle->getStartColor()->getRGB();
$css['background-color'] = $value;
$html .= ' </body>' . "\r\n";
$html .= '</html>' . "\r\n";
* @param PHPExcel_Worksheet $pSheet The worksheet for the table we are writing
$sheetIndex = $pSheet->getParent()->getIndex($pSheet);
$gridlines = $pSheet->getShowGridLines() ? ' gridlines' : '';
$html .= ' <table border="0" cellpadding="0" cellspacing="0" class="sheet' . $sheetIndex . $gridlines . '">' . "\r\n";
$html .= ' <table border="0" cellpadding="0" cellspacing="0" style="' . $style . '">' . "\r\n";
for ($i = 0; $i <= $highestColumnIndex; ++ $i) {
$html .= ' <col class="col' . $i . '">' . "\r\n";
$style = isset ($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) ?
$html .= ' <col style="' . $style . '">' . "\r\n";
$html .= ' </table>' . "\r\n";
* @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
* @param array $pValues Array containing cells in a row
* @param int $pRow Row number
private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
$sheetIndex = $pSheet->getParent()->getIndex($pSheet);
$html .= ' <tr class="row' . $pRow . '">' . "\r\n";
$style = isset ($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow])
$html .= ' <tr style="' . $style . '">' . "\r\n";
foreach ($pValues as $cell) {
$cssClass = 'column' . $colNum;
if (isset ($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
$this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
$writeCell = true; // Write cell
// Loop trough rich text elements
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
if ($element->getFont()->getSuperScript()) {
} else if ($element->getFont()->getSubScript()) {
// Convert UTF8 data to PCDATA
$cellText = $element->getText();
if ($element->getFont()->getSuperScript()) {
} else if ($element->getFont()->getSubScript()) {
$cell->getCalculatedValue(),
$pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
$pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
// Convert UTF8 data to PCDATA
// replace leading spaces on each line with
// convert newline "\n" to '<br>'
$cssClass .= ' style' . $cell->getXfIndex();
$cssClass .= ' ' . $cell->getDataType();
if (isset ($this->_cssStyles['td.style' . $cell->getXfIndex()])) {
// General horizontal alignment: Actual horizontal alignment depends on dataType
$sharedStyle = $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() );
&& isset ($this->_cssStyles['.' . $cell->getDataType()]['text-align']))
$cssClass['text-align'] = $this->_cssStyles['.' . $cell->getDataType()]['text-align'];
if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
$cellData = '<a href="' . htmlspecialchars($cell->getHyperlink()->getUrl()) . '" title="' . htmlspecialchars($cell->getHyperlink()->getTooltip()) . '">' . $cellData . '</a>';
foreach ($pSheet->getMergeCells() as $cells) {
if ($cell->isInRange($cells)) {
if ($first[0] == $cell->getCoordinate()) {
$html .= ' class="' . $cssClass . '"';
//** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
// We must explicitly write the width of the <td> element because TCPDF
// does not recognize e.g. <col style="width:42pt">
for ($i = $columnIndex; $i < $columnIndex + $colSpan; ++ $i) {
$cssClass['width'] = $width . 'pt';
// We must also explicitly write the height of the <td> element because TCPDF
// does not recognize e.g. <tr style="height:50pt">
if (isset ($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
$height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
$cssClass['height'] = $height;
//** end of redundant code **
$html .= ' colspan="' . $colSpan . '"';
$html .= ' rowspan="' . $rowSpan . '"';
$html .= '</td>' . "\r\n";
$html .= ' </tr>' . "\r\n";
throw new Exception("Invalid parameters passed.");
* Takes array where of CSS properties / values and converts to CSS string
foreach ($pValue as $property => $value) {
$pairs[] = $property . ':' . $value;
* Get Pre-Calculate Formulas
* Set Pre-Calculate Formulas
* @param boolean $pValue Pre-Calculate Formulas?
* @return PHPExcel_Writer_HTML
* @return PHPExcel_Writer_HTML
* @return PHPExcel_Writer_HTML
* Converts a string so that spaces occuring at beginning of each new line are replaced by
* Example: " Hello\n to the world" is converted to " Hello\n to the world"
$explodes = explode("\n", $pValue);
foreach ($explodes as $explode) {
$string = implode("\n", $implodes);
|