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.6.5, 2009-01-05
require_once 'PHPExcel/Writer/IWriter.php';
require_once 'PHPExcel/Cell.php';
require_once 'PHPExcel/RichText.php';
/** PHPExcel_Shared_Drawing */
require_once 'PHPExcel/Shared/Drawing.php';
/** PHPExcel_Shared_String */
require_once 'PHPExcel/Shared/String.php';
/** PHPExcel_HashTable */
require_once 'PHPExcel/HashTable.php';
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* 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 ' baseline';
default: return '1px solid'; // map others to thin
* @param int $pValue Sheet index
* 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) {
$hashCode = $sheet->getHashCode();
$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);
* 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">' . "\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.');
if ($generateSurroundingHTML) {
$html .= ' <style type="text/css">' . "\r\n";
$html .= ' html {' . "\r\n";
$html .= ' font-family: Calibri, Arial, Helvetica, sans-serif;' . "\r\n";
$html .= ' font-size: 10pt;' . "\r\n";
$html .= ' background-color: white;' . "\r\n";
// Write styles per sheet
foreach ($sheets as $sheet) {
$hashCode = $sheet->getHashCode();
$html .= ' table.sheet' . $hashCode . ', table.sheet' . $hashCode . ' td {' . "\r\n";
if ($sheet->getShowGridlines()) {
$html .= ' border: 1px dotted black;' . "\r\n";
$html .= ' page-break-after: always;' . "\r\n";
$columnDimension = $sheet->getDefaultColumnDimension();
$html .= ' table.sheet' . $hashCode . ' td {' . "\r\n";
if ($columnDimension->getVisible() === false) {
$html .= ' display: none;' . "\r\n";
$html .= ' visibility: hidden;' . "\r\n";
// Calculate column widths
$sheet->calculateColumnWidths();
foreach ($sheet->getColumnDimensions() as $columnDimension) {
$html .= ' table.sheet' . $hashCode . ' td.column' . $column . ' {' . "\r\n";
if ($columnDimension->getVisible() === false) {
$html .= ' display: none;' . "\r\n";
$html .= ' visibility: hidden;' . "\r\n";
$rowDimension = $sheet->getDefaultRowDimension();
$html .= ' table.sheet' . $hashCode . ' tr {' . "\r\n";
// height is disproportionately large
$html .= ' height: ' . $px_height . 'px;' . "\r\n";
if ($rowDimension->getVisible() === false) {
$html .= ' display: none;' . "\r\n";
$html .= ' visibility: hidden;' . "\r\n";
foreach ($sheet->getRowDimensions() as $rowDimension) {
$html .= ' table.sheet' . $hashCode . ' tr.row' . ($rowDimension->getRowIndex() - 1) . ' {' . "\r\n";
// height is disproportionately large
$html .= ' height: ' . $px_height . 'px;' . "\r\n";
if ($rowDimension->getVisible() === false) {
$html .= ' display: none;' . "\r\n";
$html .= ' visibility: hidden;' . "\r\n";
// Calculate cell style hashes
$aStyles = $sheet->getStyles();
$cellStyleHashes->addFromSource( $aStyles );
foreach ($aStyles as $style) {
if(isset ($addedStyles[$style->getHashIndex()])) continue;
$addedStyles[$style->getHashIndex()] = true;
if ($generateSurroundingHTML) {
$html .= ' </style>' . "\r\n";
* @param PHPExcel_Style $pStyle PHPExcel_Style
$html .= ' .style' . $pStyle->getHashIndex() . ' {' . "\r\n";
* Create CSS style (PHPExcel_Style_Alignment)
* @param PHPExcel_Style_Alignment $pStyle PHPExcel_Style_Alignment
$html .= ' vertical-align: ' . $this->_mapVAlign($pStyle->getVertical()) . ';' . "\r\n";
$html .= ' text-align: ' . $this->_mapHAlign($pStyle->getHorizontal()) . ';' . "\r\n";
* Create CSS style (PHPExcel_Style_Font)
* @param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
if ($pStyle->getBold()) {
$html .= ' font-weight: bold;' . "\r\n";
if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStriketrough()) {
$html .= ' text-decoration: underline line-through;' . "\r\n";
$html .= ' text-decoration: underline;' . "\r\n";
} else if ($pStyle->getStriketrough()) {
$html .= ' text-decoration: line-through;' . "\r\n";
if ($pStyle->getItalic()) {
$html .= ' font-style: italic;' . "\r\n";
$html .= ' color: ' . '#' . $pStyle->getColor()->getRGB() . ';' . "\r\n";
$html .= ' font-family: ' . '\'' . $pStyle->getName() . '\';' . "\r\n";
$html .= ' font-size: ' . $pStyle->getSize() . 'pt;' . "\r\n";
* 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
$html .= $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
* Create CSS style (PHPExcel_Style_Fill)
* @param PHPExcel_Style_Fill $pStyle PHPExcel_Style_Fill
$html .= ' background-color: ' . '#' . $pStyle->getStartColor()->getRGB() . ';' . "\r\n";
$html .= ' </body>' . "\r\n";
$html .= '</html>' . "\r\n";
* @param string $pIdentifier Identifier for the table
$html .= ' <table border="0" cellpadding="0" cellspacing="0" class="sheet' . $pIdentifier . '">' . "\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) {
$html .= ' <tr class="row' . $pRow . '">' . "\r\n";
foreach ($pValues as $cell) {
$cssClass = 'column' . $colNum;
$writeCell = true; // Write cell
// Loop trough rich text elements
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
$cellData .= '<span style="' .
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->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
$pSheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
// Convert UTF8 data to PCDATA
$cssClass .= ' style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex();
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 == $cell->getCoordinate()) {
$html .= ' class="' . $cssClass . '"';
$html .= ' colspan="' . $colSpan . '"';
$html .= ' rowspan="' . $rowSpan . '"';
$html .= '</td>' . "\r\n";
$html .= ' </tr>' . "\r\n";
throw new Exception("Invalid parameters passed.");
* Get Pre-Calculate Formulas
* Set Pre-Calculate Formulas
* @param boolean $pValue Pre-Calculate Formulas?
|