Source for file Excel5.php
Documentation is available at Excel5.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_Excel5
* @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';
/** PHPExcel_Writer_Excel5_Workbook */
require_once 'PHPExcel/Writer/Excel5/Workbook.php';
require_once 'PHPExcel/RichText.php';
/** PHPExcel_HashTable */
require_once 'PHPExcel/HashTable.php';
* @package PHPExcel_Writer_Excel5
* @copyright Copyright (c) 2006 - 2009 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) {
// check for iconv support
throw new Exception("Cannot write .xls file without PHP support for iconv");
$workbook->setVersion(8);
foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
$phpSheet = $phpExcel->getSheet($sheetIndex);
$worksheet = $workbook->addWorksheet($sheetName, $phpSheet);
$allWorksheets = $workbook->worksheets();
foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
$phpSheet = $phpExcel->getSheet($sheetIndex);
$worksheet = $allWorksheets[$sheetIndex];
$emptyStyle = $phpSheet->getDefaultStyle();
$aStyles = $phpSheet->getStyles();
// Calculate cell style hashes
$aStyles = $phpSheet->getStyles();
$cellStyleHashes->addFromSource( $aStyles );
foreach ($aStyles as $style) {
$styleHashIndex = $style->getHashIndex();
if(isset ($addedStyles[$styleHashIndex])) continue;
$formats[$styleHashIndex] = $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[$styleHashIndex]->setTextWrap();
$formats[$styleHashIndex]->setIndent($style->getAlignment()->getIndent());
if ($style->getAlignment()->getShrinkToFit()) {
$formats[$styleHashIndex]->setShrinkToFit();
if ($style->getFont()->getItalic()) {
$formats[$styleHashIndex]->setItalic();
if ($style->getFont()->getStriketrough()) {
$formats[$styleHashIndex]->setStrikeOut();
$formats[$styleHashIndex]->setUnlocked();
$formats[$styleHashIndex]->setHidden();
$addedStyles[$style->getHashIndex()] = true;
if ($sheetIndex == $phpExcel->getActiveSheetIndex()) {
// 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()];
$styleHashIndex = $style->getHashIndex();
$worksheet->write($row, $column, $cell->getValue()->getPlainText(), $formats[$styleHashIndex]);
switch ($cell->getDatatype()) {
if ($cell->getValue() === '' or $cell->getValue() === null) {
$worksheet->writeBlank($row, $column, $formats[$styleHashIndex]);
$worksheet->writeString($row, $column, $cell->getValue(), $formats[$styleHashIndex]);
$worksheet->writeFormula($row, $column, $cell->getValue(), $formats[$styleHashIndex]);
$worksheet->writeBoolErr($row, $column, $cell->getValue(), 0, $formats[$styleHashIndex]);
$worksheet->writeBoolErr($row, $column, $this->_mapErrorCode($cell->getValue()), 1, $formats[$styleHashIndex]);
$worksheet->write($row, $column, $cell->getValue(), $formats[$styleHashIndex], $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();
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());
foreach ($phpSheet->getRowDimensions() as $rowDimension) {
$worksheet->setRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), null, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel() );
foreach ($phpSheet->getDrawingCollection() as $drawing) {
$filename = $drawing->getPath();
list ($imagesx, $imagesy, $imageFormat) = getimagesize($filename);
$image = $drawing->getImageResource();
$worksheet->insertBitmap($row - 1, PHPExcel_Cell::columnIndexFromString($column) - 1, $image, $drawing->getOffsetX(), $drawing->getOffsetY(), $drawing->getWidth() / $imagesx, $drawing->getHeight() / $imagesy);
private function _addColor($workbook, $rgb) {
if (!isset ($this->_colors[$rgb])) {
return 0; // map others to none
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");
|