PHPExcel_Writer_Excel5
[ class tree: PHPExcel_Writer_Excel5 ] [ index: PHPExcel_Writer_Excel5 ] [ all elements ]

Source for file Excel5.php

Documentation is available at Excel5.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Writer_Excel5
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IWriter */
  30. require_once 'PHPExcel/Writer/IWriter.php';
  31.  
  32. /** PHPExcel_Cell */
  33. require_once 'PHPExcel/Cell.php';
  34.  
  35. /** PHPExcel_Writer_Excel5_Workbook */
  36. require_once 'PHPExcel/Writer/Excel5/Workbook.php';
  37.  
  38. /** PHPExcel_RichText */
  39. require_once 'PHPExcel/RichText.php';
  40.  
  41. /** PHPExcel_HashTable */
  42. require_once 'PHPExcel/HashTable.php';
  43.  
  44.  
  45. /**
  46.  * PHPExcel_Writer_Excel5
  47.  *
  48.  * @category   PHPExcel
  49.  * @package    PHPExcel_Writer_Excel5
  50.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  51.  */
  52. class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter {
  53.     /**
  54.      * PHPExcel object
  55.      *
  56.      * @var PHPExcel 
  57.      */
  58.     private $_phpExcel;
  59.  
  60.     /**
  61.      * Temporary storage directory
  62.      *
  63.      * @var string 
  64.      */
  65.     private $_tempDir = '';
  66.  
  67.     /**
  68.      * Color cache
  69.      */
  70.     private $_colors = array();
  71.  
  72.     /**
  73.      * Create a new PHPExcel_Writer_Excel5
  74.      *
  75.      * @param    PHPExcel    $phpExcel    PHPExcel object
  76.      */
  77.     public function __construct(PHPExcel $phpExcel{
  78.         $this->_phpExcel    = $phpExcel;
  79.         $this->_tempDir        = '';
  80.         $this->_colors        = array();
  81.     }
  82.  
  83.     /**
  84.      * Save PHPExcel to file
  85.      *
  86.      * @param    string        $pFileName 
  87.      * @throws    Exception
  88.      */
  89.     public function save($pFilename null{
  90.  
  91.         // check for iconv support
  92.         if (!function_exists('iconv')) {
  93.             throw new Exception("Cannot write .xls file without PHP support for iconv");
  94.         }
  95.  
  96.         $this->_colors        = array();
  97.  
  98.         $phpExcel $this->_phpExcel;
  99.         $workbook new PHPExcel_Writer_Excel5_Workbook($pFilename$phpExcel);
  100.         $workbook->setVersion(8);
  101.  
  102.         // Set temp dir
  103.         if ($this->_tempDir != ''{
  104.             $workbook->setTempDir($this->_tempDir);
  105.         }
  106.  
  107.         $saveDateReturnType PHPExcel_Calculation_Functions::getReturnDateType();
  108.  
  109.         // Add empty sheets
  110.         foreach ($phpExcel->getSheetNames(as $sheetIndex => $sheetName{
  111.             $phpSheet  $phpExcel->getSheet($sheetIndex);
  112.             $worksheet $workbook->addWorksheet($sheetName$phpSheet);
  113.         }
  114.         $allWorksheets $workbook->worksheets();
  115.  
  116.         $formats array();
  117.  
  118.         // Add full sheet data
  119.         foreach ($phpExcel->getSheetNames(as $sheetIndex => $sheetName{
  120.             $phpSheet  $phpExcel->getSheet($sheetIndex);
  121.             $worksheet $allWorksheets[$sheetIndex];
  122.             
  123.             // Default style
  124.             $emptyStyle $phpSheet->getDefaultStyle();
  125.  
  126.             $aStyles $phpSheet->getStyles();
  127.             
  128.             // Calculate cell style hashes
  129.             $cellStyleHashes new PHPExcel_HashTable();
  130.             $aStyles $phpSheet->getStyles();
  131.             $cellStyleHashes->addFromSource$aStyles );
  132.  
  133.             $addedStyles array();
  134.             foreach ($aStyles as $style{
  135.                 $styleHashIndex $style->getHashIndex();
  136.  
  137.                 if(isset($addedStyles[$styleHashIndex])) continue;
  138.                 
  139.                 $formats[$styleHashIndex$workbook->addFormat(array(
  140.                     'HAlign' => $style->getAlignment()->getHorizontal(),
  141.                     'VAlign' => $this->_mapVAlign($style->getAlignment()->getVertical()),
  142.                     'TextRotation' => $style->getAlignment()->getTextRotation(),
  143.  
  144.                     'Bold' => $style->getFont()->getBold(),
  145.                     'FontFamily' => $style->getFont()->getName(),
  146.                     'Color' => $this->_addColor($workbook$style->getFont()->getColor()->getRGB()),
  147.                     'Underline' => $this->_mapUnderline($style->getFont()->getUnderline()),
  148.                     'Size' => $style->getFont()->getSize(),
  149.                     //~ 'Script' => $style->getSuperscript(),
  150.  
  151.                     'NumFormat' => $style->getNumberFormat()->getFormatCode(),
  152.  
  153.                     'Bottom' => $this->_mapBorderStyle($style->getBorders()->getBottom()->getBorderStyle()),
  154.                     'Top' => $this->_mapBorderStyle($style->getBorders()->getTop()->getBorderStyle()),
  155.                     'Left' => $this->_mapBorderStyle($style->getBorders()->getLeft()->getBorderStyle()),
  156.                     'Right' => $this->_mapBorderStyle($style->getBorders()->getRight()->getBorderStyle()),
  157.                     'BottomColor' => $this->_addColor($workbook$style->getBorders()->getBottom()->getColor()->getRGB()),
  158.                     'TopColor' => $this->_addColor($workbook$style->getBorders()->getTop()->getColor()->getRGB()),
  159.                     'RightColor' => $this->_addColor($workbook$style->getBorders()->getRight()->getColor()->getRGB()),
  160.                     'LeftColor' => $this->_addColor($workbook$style->getBorders()->getLeft()->getColor()->getRGB()),
  161.  
  162.                     'FgColor' => $this->_addColor($workbook$style->getFill()->getStartColor()->getRGB()),
  163.                     'BgColor' => $this->_addColor($workbook$style->getFill()->getEndColor()->getRGB()),
  164.                     'Pattern' => $this->_mapFillType($style->getFill()->getFillType()),
  165.  
  166.                 ));
  167.                 if ($style->getAlignment()->getWrapText()) {
  168.                     $formats[$styleHashIndex]->setTextWrap();
  169.                 }
  170.                 $formats[$styleHashIndex]->setIndent($style->getAlignment()->getIndent());
  171.                 if ($style->getAlignment()->getShrinkToFit()) {
  172.                     $formats[$styleHashIndex]->setShrinkToFit();
  173.                 }
  174.                 if ($style->getFont()->getItalic()) {
  175.                     $formats[$styleHashIndex]->setItalic();
  176.                 }
  177.                 if ($style->getFont()->getStriketrough()) {
  178.                     $formats[$styleHashIndex]->setStrikeOut();
  179.                 }
  180.                 if ($style->getProtection()->getLocked(== PHPExcel_Style_Protection::PROTECTION_UNPROTECTED{
  181.                     $formats[$styleHashIndex]->setUnlocked();
  182.                 }
  183.                 if ($style->getProtection()->getHidden(== PHPExcel_Style_Protection::PROTECTION_PROTECTED{
  184.                     $formats[$styleHashIndex]->setHidden();
  185.                 }
  186.                 
  187.                 $addedStyles[$style->getHashIndex()true;
  188.             }
  189.  
  190.             // Active sheet
  191.             if ($sheetIndex == $phpExcel->getActiveSheetIndex()) {
  192.                 $worksheet->activate();
  193.             }
  194.  
  195.             // initialize first, last, row and column index, needed for DIMENSION record
  196.             $firstRowIndex 0;
  197.             $lastRowIndex = -1;
  198.             $firstColumnIndex 0;
  199.             $lastColumnIndex = -1;
  200.  
  201.             foreach ($phpSheet->getCellCollection(as $cell{
  202.                 $row $cell->getRow(1;
  203.                 $column PHPExcel_Cell::columnIndexFromString($cell->getColumn()) 1;
  204.  
  205.                 // Don't break Excel!
  206.                 if ($row >= 65569{
  207.                     break;
  208.                 }
  209.  
  210.                 $firstRowIndex min($firstRowIndex$row);
  211.                 $lastRowIndex max($lastRowIndex$row);
  212.                 $firstColumnIndex min($firstColumnIndex$column);
  213.                 $lastColumnIndex max($lastColumnIndex$column);
  214.  
  215.                 $style $emptyStyle;
  216.                 if (isset($aStyles[$cell->getCoordinate()])) {
  217.                     $style $aStyles[$cell->getCoordinate()];
  218.                 }
  219.                 $styleHashIndex $style->getHashIndex();
  220.  
  221.                 // Write cell value
  222.                 if ($cell->getValue(instanceof PHPExcel_RichText{
  223.                     $worksheet->write($row$column$cell->getValue()->getPlainText()$formats[$styleHashIndex]);
  224.                 else {
  225.                     switch ($cell->getDatatype()) {
  226.  
  227.                     case PHPExcel_Cell_DataType::TYPE_STRING:
  228.                         if ($cell->getValue(=== '' or $cell->getValue(=== null{
  229.                             $worksheet->writeBlank($row$column$formats[$styleHashIndex]);
  230.                         else {
  231.                             $worksheet->writeString($row$column$cell->getValue()$formats[$styleHashIndex]);
  232.                         }
  233.                         break;
  234.  
  235.                     case PHPExcel_Cell_DataType::TYPE_FORMULA:
  236.                         $worksheet->writeFormula($row$column$cell->getValue()$formats[$styleHashIndex]);
  237.                         break;
  238.  
  239.                     case PHPExcel_Cell_DataType::TYPE_BOOL:
  240.                         $worksheet->writeBoolErr($row$column$cell->getValue()0$formats[$styleHashIndex]);
  241.                         break;
  242.  
  243.                     case PHPExcel_Cell_DataType::TYPE_ERROR:
  244.                         $worksheet->writeBoolErr($row$column$this->_mapErrorCode($cell->getValue())1$formats[$styleHashIndex]);
  245.                         break;
  246.  
  247.                     default:
  248.                         $worksheet->write($row$column$cell->getValue()$formats[$styleHashIndex]$style->getNumberFormat()->getFormatCode());
  249.                         break;
  250.                     }
  251.  
  252.                     // Hyperlink?
  253.                     if ($cell->hasHyperlink()) {
  254.                         $worksheet->writeUrl($row$columnstr_replace('sheet://''internal:'$cell->getHyperlink()->getUrl()));
  255.                     }
  256.                 }
  257.             }
  258.  
  259.             // set the worksheet dimension for the DIMENSION record
  260.             $worksheet->setDimensions($firstRowIndex$lastRowIndex$firstColumnIndex$lastColumnIndex);
  261.  
  262.             $phpSheet->calculateColumnWidths();
  263.  
  264.             // Column dimensions
  265.             foreach ($phpSheet->getColumnDimensions(as $columnDimension{
  266.                 $column PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) 1;
  267.                 if ($column 256{
  268.                     if ($columnDimension->getWidth(>= 0{
  269.                         $width $columnDimension->getWidth();
  270.                     else if ($phpSheet->getDefaultColumnDimension()->getWidth(>= 0{
  271.                         $width $phpSheet->getDefaultColumnDimension()->getWidth();
  272.                     else {
  273.                         $width 8;
  274.                     }
  275.                     $worksheet->setColumn$column$column$widthnull($columnDimension->getVisible('0' '1')$columnDimension->getOutlineLevel());
  276.                 }
  277.             }
  278.  
  279.             // Row dimensions
  280.             foreach ($phpSheet->getRowDimensions(as $rowDimension{
  281.                 $worksheet->setRow$rowDimension->getRowIndex(1$rowDimension->getRowHeight()null($rowDimension->getVisible('0' '1')$rowDimension->getOutlineLevel() );
  282.             }
  283.  
  284.             foreach ($phpSheet->getDrawingCollection(as $drawing{
  285.                 list($column$rowPHPExcel_Cell::coordinateFromString($drawing->getCoordinates());
  286.  
  287.                 if ($drawing instanceof PHPExcel_Worksheet_Drawing{
  288.                     $filename $drawing->getPath();
  289.                     list($imagesx$imagesy$imageFormatgetimagesize($filename);
  290.                     switch ($imageFormat{
  291.                         case 1$image imagecreatefromgif($filename)break;
  292.                         case 2$image imagecreatefromjpeg($filename)break;
  293.                         case 3$image imagecreatefrompng($filename)break;
  294.                         defaultcontinue 2;
  295.                     }
  296.                 else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing{
  297.                     $image $drawing->getImageResource();
  298.                     $imagesx imagesx($image);
  299.                     $imagesy imagesy($image);
  300.                 }
  301.  
  302.                 $worksheet->insertBitmap($row 1PHPExcel_Cell::columnIndexFromString($column1$image$drawing->getOffsetX()$drawing->getOffsetY()$drawing->getWidth($imagesx$drawing->getHeight($imagesy);
  303.             }
  304.         }
  305.  
  306.         PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  307.  
  308.         $workbook->close();
  309.     }
  310.  
  311.     /**
  312.      * Add color
  313.      */
  314.     private function _addColor($workbook$rgb{
  315.         if (!isset($this->_colors[$rgb])) {
  316.             $workbook->setCustomColor(count($this->_colors)hexdec(substr($rgb02))hexdec(substr($rgb22))hexdec(substr($rgb4)));
  317.             $this->_colors[$rgbcount($this->_colors);
  318.         }
  319.         return $this->_colors[$rgb];
  320.     }
  321.  
  322.     /**
  323.      * Map border style
  324.      */
  325.     private function _mapBorderStyle($borderStyle{
  326.         switch ($borderStyle{
  327.             case PHPExcel_Style_Border::BORDER_NONE:                return 0x00;
  328.             case PHPExcel_Style_Border::BORDER_THIN;                return 0x01;
  329.             case PHPExcel_Style_Border::BORDER_MEDIUM;                return 0x02;
  330.             case PHPExcel_Style_Border::BORDER_DASHED;                return 0x03;
  331.             case PHPExcel_Style_Border::BORDER_DOTTED;                return 0x04;
  332.             case PHPExcel_Style_Border::BORDER_THICK;                return 0x05;
  333.             case PHPExcel_Style_Border::BORDER_DOUBLE;                return 0x06;
  334.             case PHPExcel_Style_Border::BORDER_HAIR;                return 0x07;
  335.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHED;        return 0x08;
  336.             case PHPExcel_Style_Border::BORDER_DASHDOT;                return 0x09;
  337.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;        return 0x0A;
  338.             case PHPExcel_Style_Border::BORDER_DASHDOTDOT;            return 0x0B;
  339.             case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;    return 0x0C;
  340.             case PHPExcel_Style_Border::BORDER_SLANTDASHDOT;        return 0x0D;
  341.             default:                                                return 0x00;
  342.         }
  343.     }
  344.  
  345.     /**
  346.      * Map underline
  347.      */
  348.     private function _mapUnderline($underline{
  349.         switch ($underline{
  350.             case PHPExcel_Style_Font::UNDERLINE_NONE:
  351.                 return 0;
  352.             case PHPExcel_Style_Font::UNDERLINE_SINGLE:
  353.                 return 1;
  354.             case PHPExcel_Style_Font::UNDERLINE_DOUBLE:
  355.                 return 2;
  356.             case PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING:
  357.                 return 0x21;
  358.             case PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING:
  359.                 return 0x22;
  360.             default:
  361.                 return 0// map others to none
  362.         }
  363.     }
  364.  
  365.     /**
  366.      * Map fill type
  367.      */
  368.     private function _mapFillType($fillType{
  369.         switch ($fillType{
  370.             case PHPExcel_Style_Fill::FILL_NONE:                    return 0x00;
  371.             case PHPExcel_Style_Fill::FILL_SOLID:                    return 0x01;
  372.             case PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY:        return 0x02;
  373.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY:        return 0x03;
  374.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY:        return 0x04;
  375.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL:    return 0x05;
  376.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL:    return 0x06;
  377.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN:        return 0x07;
  378.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKUP:            return 0x08;
  379.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID:        return 0x09;
  380.             case PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS:        return 0x0A;
  381.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL:    return 0x0B;
  382.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL:    return 0x0C;
  383.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN:        return 0x0D;
  384.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP:            return 0x0E;
  385.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID:        return 0x0F;
  386.             case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS:    return 0x10;
  387.             case PHPExcel_Style_Fill::FILL_PATTERN_GRAY125:            return 0x11;
  388.             case PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625:        return 0x12;
  389.             case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR:        // does not exist in BIFF8
  390.             case PHPExcel_Style_Fill::FILL_GRADIENT_PATH:        // does not exist in BIFF8
  391.             default:                                                return 0x00;
  392.         }
  393.     }
  394.  
  395.     /**
  396.      * Map VAlign
  397.      */
  398.     private function _mapVAlign($vAlign{
  399.         return ($vAlign == 'center' || $vAlign == 'justify' 'v' ''$vAlign;
  400.     }
  401.  
  402.     /**
  403.      * Map Error code
  404.      */
  405.     private function _mapErrorCode($errorCode{
  406.         switch ($errorCode{
  407.             case '#NULL!':    return 0x00;
  408.             case '#DIV/0!':    return 0x07;
  409.             case '#VALUE!':    return 0x0F;
  410.             case '#REF!':    return 0x17;
  411.             case '#NAME?':    return 0x1D;
  412.             case '#NUM!':    return 0x24;
  413.             case '#N/A':    return 0x2A;
  414.         }
  415.  
  416.         return 0;
  417.     }
  418.  
  419.     /**
  420.      * Get an array of all styles
  421.      *
  422.      * @param    PHPExcel                $pPHPExcel 
  423.      * @return    PHPExcel_Style[]        All styles in PHPExcel
  424.      * @throws    Exception
  425.      */
  426.     private function _allStyles(PHPExcel $pPHPExcel null)
  427.     {
  428.         // Get an array of all styles
  429.         $aStyles        array();
  430.  
  431.         for ($i 0$i $pPHPExcel->getSheetCount()++$i{
  432.             foreach ($pPHPExcel->getSheet($i)->getStyles(as $style{
  433.                 $aStyles[$style;
  434.             }
  435.         }
  436.  
  437.         return $aStyles;
  438.     }
  439.  
  440.     /**
  441.      * Get temporary storage directory
  442.      *
  443.      * @return string 
  444.      */
  445.     public function getTempDir({
  446.         return $this->_tempDir;
  447.     }
  448.  
  449.     /**
  450.      * Set temporary storage directory
  451.      *
  452.      * @param    string    $pValue        Temporary storage directory
  453.      * @throws    Exception    Exception when directory does not exist
  454.      */
  455.     public function setTempDir($pValue ''{
  456.         if (is_dir($pValue)) {
  457.             $this->_tempDir = $pValue;
  458.         else {
  459.             throw new Exception("Directory does not exist: $pValue");
  460.         }
  461.     }
  462. }

Documentation generated on Mon, 05 Jan 2009 20:36:56 +0100 by phpDocumentor 1.4.1