Source for file Excel5.php
Documentation is available at Excel5.php
* Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.6.3, 2008-08-25
require_once 'PHPExcel/Writer/IWriter.php';
require_once 'PHPExcel/Cell.php';
/** PHPExcel_Writer_Excel5_Writer */
require_once 'PHPExcel/Writer/Excel5/Writer.php';
require_once 'PHPExcel/RichText.php';
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
* Temporary storage directory
* Create a new PHPExcel_Writer_Excel5
* @param PHPExcel $phpExcel PHPExcel object
* @param string $pFileName
public function save($pFilename = null) {
$workbook->setVersion(8);
foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
$phpSheet = $phpExcel->getSheet($sheetIndex);
$worksheet = $workbook->addWorksheet($sheetName);
$allWorksheets = $workbook->worksheets();
foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
$phpSheet = $phpExcel->getSheet($sheetIndex);
$worksheet = $allWorksheets[$sheetIndex];
$worksheet->setInputEncoding("UTF-8");
$emptyStyle = $phpSheet->getDefaultStyle();
$aStyles = $phpSheet->getStyles();
$freeze = $phpSheet->getFreezePane();
//if ($sheetIndex == $phpExcel->getActiveSheetIndex()) {
if ($phpSheet->getProtection()->getSheet()) {
$worksheet->protect($phpSheet->getProtection()->getPassword(), true);
if (!$phpSheet->getShowGridlines()) {
$worksheet->hideGridLines();
foreach ($phpSheet->getCellCollection() as $cell) {
$row = $cell->getRow() - 1;
if (isset ($aStyles[$cell->getCoordinate()])) {
$style = $aStyles[$cell->getCoordinate()];
$styleHash = $style->getHashCode();
if (!isset ($formats[$styleHash])) {
$formats[$styleHash] = $workbook->addFormat(array(
'HAlign' => $style->getAlignment()->getHorizontal(),
'VAlign' => $this->_mapVAlign($style->getAlignment()->getVertical()),
'TextRotation' => $style->getAlignment()->getTextRotation(),
'Bold' => $style->getFont()->getBold(),
'FontFamily' => $style->getFont()->getName(),
'Color' => $this->_addColor($workbook, $style->getFont()->getColor()->getRGB()),
'Underline' => $this->_mapUnderline($style->getFont()->getUnderline()),
'Size' => $style->getFont()->getSize(),
//~ 'Script' => $style->getSuperscript(),
'NumFormat' => iconv("UTF-8", "Windows-1252", $style->getNumberFormat()->getFormatCode()),
'Bottom' => $this->_mapBorderStyle($style->getBorders()->getBottom()->getBorderStyle()),
'Top' => $this->_mapBorderStyle($style->getBorders()->getTop()->getBorderStyle()),
'Left' => $this->_mapBorderStyle($style->getBorders()->getLeft()->getBorderStyle()),
'Right' => $this->_mapBorderStyle($style->getBorders()->getRight()->getBorderStyle()),
'BottomColor' => $this->_addColor($workbook, $style->getBorders()->getBottom()->getColor()->getRGB()),
'TopColor' => $this->_addColor($workbook, $style->getBorders()->getTop()->getColor()->getRGB()),
'RightColor' => $this->_addColor($workbook, $style->getBorders()->getRight()->getColor()->getRGB()),
'LeftColor' => $this->_addColor($workbook, $style->getBorders()->getLeft()->getColor()->getRGB()),
'FgColor' => $this->_addColor($workbook, $style->getFill()->getStartColor()->getRGB()),
'BgColor' => $this->_addColor($workbook, $style->getFill()->getEndColor()->getRGB()),
'Pattern' => $this->_mapFillType($style->getFill()->getFillType()),
if ($style->getAlignment()->getWrapText()) {
$formats[$styleHash]->setTextWrap();
if ($style->getAlignment()->getShrinkToFit()) {
$formats[$styleHash]->setShrinkToFit();
if ($style->getFont()->getItalic()) {
$formats[$styleHash]->setItalic();
if ($style->getFont()->getStriketrough()) {
$formats[$styleHash]->setStrikeOut();
$worksheet->write($row, $column, $cell->getValue()->getPlainText(), $formats[$styleHash]);
if ($cell->hasHyperlink()) {
$worksheet->writeUrl($row, $column, str_replace('sheet://', 'internal:', $cell->getHyperlink()->getUrl()), $cell->getValue(), $formats[$styleHash]);
$worksheet->write($row, $column, $cell->getValue(), $formats[$styleHash],$style->getNumberFormat()->getFormatCode());
$phpSheet->calculateColumnWidths();
if ($phpSheet->getDefaultColumnDimension()->getWidth() >= 0) {
$defaultWidth = $phpSheet->getDefaultColumnDimension()->getWidth();
$worksheet->setColumn( $column, $column, $defaultWidth );
foreach ($phpSheet->getColumnDimensions() as $columnDimension) {
$worksheet->setColumn( $column, $column, $columnDimension->getWidth(), null, ($columnDimension->getVisible() ? '0' : '1'), $columnDimension->getOutlineLevel());
if ($phpSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
$defaultHeight = $phpSheet->getDefaultRowDimension()->getRowHeight();
for ($i = 0; $i < $phpSheet->getHighestRow() - 1; $i++ ) {
$worksheet->setRow( $i, $defaultHeight );
foreach ($phpSheet->getRowDimensions() as $rowDimension) {
$worksheet->setRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), null, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel() );
foreach ($phpSheet->getMergeCells() as $cells) {
foreach ($phpSheet->getDrawingCollection() as $drawing) {
$filename = $drawing->getPath();
$worksheet->insertBitmap($row - 1, PHPExcel_Cell::columnIndexFromString($column) - 1, $image, $drawing->getOffsetX(), $drawing->getOffsetY(), $drawing->getWidth() / $imagesize[0], $drawing->getHeight() / $imagesize[1]);
$worksheet->setLandscape();
$worksheet->setPaper($phpSheet->getPageSetup()->getPaperSize());
$worksheet->setHeader($phpSheet->getHeaderFooter()->getOddHeader(), $phpSheet->getPageMargins()->getHeader());
$worksheet->setFooter($phpSheet->getHeaderFooter()->getOddFooter(), $phpSheet->getPageMargins()->getFooter());
$worksheet->setMarginLeft($phpSheet->getPageMargins()->getLeft());
$worksheet->setMarginRight($phpSheet->getPageMargins()->getRight());
$worksheet->setMarginTop($phpSheet->getPageMargins()->getTop());
$worksheet->setMarginBottom($phpSheet->getPageMargins()->getBottom());
// -------------------------------------------------------------------
// http://pear.php.net/bugs/bug.php?id=2146
// -------------------------------------------------------------------
// // repeatColumns / repeatRows
// if ($phpSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $phpSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
// if ($phpSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
// $repeat = $phpSheet->getPageSetup()->getColumnsToRepeatAtLeft();
// $worksheet->repeatColumns(PHPExcel_Cell::columnIndexFromString($repeat[0] - 1), PHPExcel_Cell::columnIndexFromString($repeat[1] - 1));
// if ($phpSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
// $repeat = $phpSheet->getPageSetup()->getRowsToRepeatAtTop();
// $worksheet->repeatRows($repeat[0], $repeat[1]);
// -------------------------------------------------------------------
// http://pear.php.net/bugs/bug.php?id=2146
// -------------------------------------------------------------------
// if ($phpSheet->getPageSetup()->isPrintAreaSet()) {
// $printArea = PHPExcel_Cell::splitRange($phpSheet->getPageSetup()->getPrintArea());
// $printArea[0] = PHPExcel_Cell::coordinateFromString($printArea[0]);
// $printArea[1] = PHPExcel_Cell::coordinateFromString($printArea[1]);
// $worksheet->printArea(
// PHPExcel_Cell::columnIndexFromString($printArea[0][0]) - 1,
// PHPExcel_Cell::columnIndexFromString($printArea[1][0]) - 1
// Support for print scale
if ($phpSheet->getPageSetup()->getScale()) {
$worksheet->setPrintScale($phpSheet->getPageSetup()->getScale());
// Support for fitting to pages
if ($phpSheet->getPageSetup()->getFitToWidth()) {
if ($phpSheet->getPageSetup()->getFitToHeight()) {
// Both properties are set, so use them
// Note: This case is double, see below
$worksheet->fitToPages($phpSheet->getPageSetup()->getFitToWidth(), $phpSheet->getPageSetup()->getFitToHeight());
// Only width given, make assumption about height
$worksheet->fitToPages($phpSheet->getPageSetup()->getFitToWidth(), $height);
} else if ($phpSheet->getPageSetup()->getFitToHeight()) {
if ($phpSheet->getPageSetup()->getFitToWidth()) {
// Both properties are set, so use them
// Note: This case is double, see below
$worksheet->fitToPages($phpSheet->getPageSetup()->getFitToWidth(), $phpSheet->getPageSetup()->getFitToHeight());
// Only height given, make assumption about width
$worksheet->fitToPages($width, $phpSheet->getPageSetup()->getFitToHeight());
foreach ($phpSheet->getBreaks() as $cell => $breakType) {
// Decide what to do by the type of break
// Add to list of vertical breaks
$vBreaks[] = $coordinates[0];
// Add to list of horizontal breaks
$hBreaks[] = $coordinates[1];
$worksheet->setVPagebreaks($vBreaks);
$worksheet->setHPagebreaks($hBreaks);
private function _addColor($workbook, $rgb) {
if (!isset ($this->_colors[$rgb])) {
default: return 1; // map others to thin
return 1; // map others to single
switch ($fillType) { // just a guess
return ($vAlign == 'center' || $vAlign == 'justify' ? 'v' : '') . $vAlign;
* Get an array of all styles
* @param PHPExcel $pPHPExcel
* @return PHPExcel_Style[] All styles in PHPExcel
private function _allStyles(PHPExcel $pPHPExcel = null)
// Get an array of all styles
for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++ ) {
foreach ($pPHPExcel->getSheet($i)->getStyles() as $style) {
* Get temporary storage directory
* Set temporary storage directory
* @param string $pValue Temporary storage directory
* @throws Exception Exception when directory does not exist
throw new Exception("Directory does not exist: $pValue");
|