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.4, 2008-10-27
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();
// initialize first, last, row and column index, needed for DIMENSION record
foreach ($phpSheet->getCellCollection() as $cell) {
$row = $cell->getRow() - 1;
$firstRowIndex = min($firstRowIndex, $row);
$lastRowIndex = max($lastRowIndex, $row);
$firstColumnIndex = min($firstColumnIndex, $column);
$lastColumnIndex = max($lastColumnIndex, $column);
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' => $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();
$formats[$styleHash]->setIndent($style->getAlignment()->getIndent());
if ($style->getAlignment()->getShrinkToFit()) {
$formats[$styleHash]->setShrinkToFit();
if ($style->getFont()->getItalic()) {
$formats[$styleHash]->setItalic();
if ($style->getFont()->getStriketrough()) {
$formats[$styleHash]->setStrikeOut();
$formats[$styleHash]->setUnlocked();
$formats[$styleHash]->setHidden();
$worksheet->write($row, $column, $cell->getValue()->getPlainText(), $formats[$styleHash]);
switch ($cell->getDatatype()) {
if ($cell->getValue() === '' or $cell->getValue() === null) {
$worksheet->writeBlank($row, $column, $formats[$styleHash]);
$worksheet->writeString($row, $column, $cell->getValue(), $formats[$styleHash]);
$worksheet->writeFormula($row, $column, $cell->getValue(), $formats[$styleHash]);
$worksheet->writeBoolErr($row, $column, $cell->getValue(), 0, $formats[$styleHash]);
$worksheet->writeBoolErr($row, $column, $this->_mapErrorCode($cell->getValue()), 1, $formats[$styleHash]);
$worksheet->write($row, $column, $cell->getValue(), $formats[$styleHash], $style->getNumberFormat()->getFormatCode());
if ($cell->hasHyperlink()) {
$worksheet->writeUrl($row, $column, str_replace('sheet://', 'internal:', $cell->getHyperlink()->getUrl()));
// set the worksheet dimension for the DIMENSION record
$worksheet->setDimensions($firstRowIndex, $lastRowIndex, $firstColumnIndex, $lastColumnIndex);
$phpSheet->calculateColumnWidths();
if ($phpSheet->getDefaultColumnDimension()->getWidth() >= 0) {
$worksheet->setDefColWidth((int) $phpSheet->getDefaultColumnDimension()->getWidth());
foreach ($phpSheet->getColumnDimensions() as $columnDimension) {
if ($columnDimension->getWidth() >= 0) {
$width = $columnDimension->getWidth();
} else if ($phpSheet->getDefaultColumnDimension()->getWidth() >= 0) {
$width = $phpSheet->getDefaultColumnDimension()->getWidth();
$worksheet->setColumn( $column, $column, $width, null, ($columnDimension->getVisible() ? '0' : '1'), $columnDimension->getOutlineLevel());
if ($phpSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
// set default row height in twips = 1/20 point
$worksheet->setDefaultRowHeight((int) 20 * $phpSheet->getDefaultRowDimension()->getRowHeight());
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());
if ($phpSheet->getPageSetup()->getHorizontalCentered()) {
$worksheet->centerHorizontally();
if ($phpSheet->getPageSetup()->getVerticalCentered()) {
$worksheet->centerVertically();
$worksheet->setMarginLeft($phpSheet->getPageMargins()->getLeft());
$worksheet->setMarginRight($phpSheet->getPageMargins()->getRight());
$worksheet->setMarginTop($phpSheet->getPageMargins()->getTop());
$worksheet->setMarginBottom($phpSheet->getPageMargins()->getBottom());
// Uncommented again: should now be fixed
// -------------------------------------------------------------------
// 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();
if ($phpSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
$repeat = $phpSheet->getPageSetup()->getRowsToRepeatAtTop();
$worksheet->repeatRows($repeat[0] - 1, $repeat[1] - 1);
// Uncommented again: should now be fixed
// -------------------------------------------------------------------
// http://pear.php.net/bugs/bug.php?id=2146
// -------------------------------------------------------------------
if ($phpSheet->getPageSetup()->isPrintAreaSet()) {
// 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;
case '#NULL!': return 0x00;
case '#DIV/0!': return 0x07;
case '#VALUE!': return 0x0F;
case '#REF!': return 0x17;
case '#NAME?': return 0x1D;
case '#NUM!': return 0x24;
case '#N/A': return 0x2A;
* 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");
|