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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.4, 2010-08-26
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_Excel5
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer_Excel5
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
  37. {
  38.     /**
  39.      * Pre-calculate formulas
  40.      *
  41.      * @var boolean 
  42.      */
  43.     private $_preCalculateFormulas;
  44.  
  45.     /**
  46.      * PHPExcel object
  47.      *
  48.      * @var PHPExcel 
  49.      */
  50.     private $_phpExcel;
  51.  
  52.     /**
  53.      * The BIFF version of the written Excel file, BIFF5 = 0x0500, BIFF8 = 0x0600
  54.      *
  55.      * @var integer 
  56.      */
  57.     private $_BIFF_version;
  58.  
  59.     /**
  60.      * Total number of shared strings in workbook
  61.      *
  62.      * @var int 
  63.      */
  64.     private $_str_total;
  65.  
  66.     /**
  67.      * Number of unique shared strings in workbook
  68.      *
  69.      * @var int 
  70.      */
  71.     private $_str_unique;
  72.  
  73.     /**
  74.      * Array of unique shared strings in workbook
  75.      *
  76.      * @var array 
  77.      */
  78.     private $_str_table;
  79.  
  80.     /**
  81.      * Color cache. Mapping between RGB value and color index.
  82.      *
  83.      * @var array 
  84.      */
  85.     private $_colors;
  86.  
  87.     /**
  88.      * Formula parser
  89.      *
  90.      * @var PHPExcel_Writer_Excel5_Parser 
  91.      */
  92.     private $_parser;
  93.  
  94.     /**
  95.      * Identifier clusters for drawings. Used in MSODRAWINGGROUP record.
  96.      *
  97.      * @var array 
  98.      */
  99.     private $_IDCLs;
  100.  
  101.  
  102.     /**
  103.      * Create a new PHPExcel_Writer_Excel5
  104.      *
  105.      * @param    PHPExcel    $phpExcel    PHPExcel object
  106.      */
  107.     public function __construct(PHPExcel $phpExcel{
  108.         $this->_preCalculateFormulas true;
  109.         $this->_phpExcel        $phpExcel;
  110.         $this->_BIFF_version    0x0600;
  111.  
  112.         $this->_str_total       0;
  113.         $this->_str_unique      0;
  114.         $this->_str_table       array();
  115.         $this->_parser          new PHPExcel_Writer_Excel5_Parser($this->_BIFF_version);
  116.  
  117.     }
  118.  
  119.     /**
  120.      * Save PHPExcel to file
  121.      *
  122.      * @param    string        $pFileName 
  123.      * @throws    Exception
  124.      */
  125.     public function save($pFilename null{
  126.  
  127.         // garbage collect
  128.         $this->_phpExcel->garbageCollect();
  129.  
  130.         $saveDebugLog PHPExcel_Calculation::getInstance()->writeDebugLog;
  131.         PHPExcel_Calculation::getInstance()->writeDebugLog false;
  132.         $saveDateReturnType PHPExcel_Calculation_Functions::getReturnDateType();
  133.  
  134.         // initialize colors array
  135.         $this->_colors          array();
  136.  
  137.         // Initialise workbook writer
  138.         $this->_writerWorkbook new PHPExcel_Writer_Excel5_Workbook($this->_phpExcel$this->_BIFF_version,
  139.                     $this->_str_total$this->_str_unique$this->_str_table$this->_colors$this->_parser);
  140.  
  141.         // Initialise worksheet writers
  142.         $countSheets $this->_phpExcel->getSheetCount();
  143.         for ($i 0$i $countSheets++$i{
  144.             $this->_writerWorksheets[$inew PHPExcel_Writer_Excel5_Worksheet($this->_BIFF_version,
  145.                                        $this->_str_total$this->_str_unique,
  146.                                        $this->_str_table$this->_colors,
  147.                                        $this->_parser,
  148.                                        $this->_preCalculateFormulas,
  149.                                        $this->_phpExcel->getSheet($i));
  150.         }
  151.  
  152.         // build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.
  153.         $this->_buildWorksheetEschers();
  154.         $this->_buildWorkbookEscher();
  155.  
  156.         // add 15 identical cell style Xfs
  157.         // for now, we use the first cellXf instead of cellStyleXf
  158.         $cellXfCollection $this->_phpExcel->getCellXfCollection();
  159.         for ($i 0$i 15++$i{
  160.             $this->_writerWorkbook->addXfWriter($cellXfCollection[0]true);
  161.         }
  162.  
  163.         // add all the cell Xfs
  164.         foreach ($this->_phpExcel->getCellXfCollection(as $style{
  165.             $this->_writerWorkbook->addXfWriter($stylefalse);
  166.         }
  167.  
  168.         // initialize OLE file
  169.         $workbookStreamName ($this->_BIFF_version == 0x0600'Workbook' 'Book';
  170.         $OLE new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs($workbookStreamName));
  171.  
  172.         // Write the worksheet streams before the global workbook stream,
  173.         // because the byte sizes of these are needed in the global workbook stream
  174.         $worksheetSizes array();
  175.         for ($i 0$i $countSheets++$i{
  176.             $this->_writerWorksheets[$i]->close();
  177.             $worksheetSizes[$this->_writerWorksheets[$i]->_datasize;
  178.         }
  179.  
  180.         // add binary data for global workbook stream
  181.         $OLE->append$this->_writerWorkbook->writeWorkbook($worksheetSizes) );
  182.  
  183.         // add binary data for sheet streams
  184.         for ($i 0$i $countSheets++$i{
  185.             $OLE->append($this->_writerWorksheets[$i]->getData());
  186.         }
  187.  
  188.         $root new PHPExcel_Shared_OLE_PPS_Root(time()time()array($OLE));
  189.         // save the OLE file
  190.         $res $root->save($pFilename);
  191.  
  192.         PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  193.         PHPExcel_Calculation::getInstance()->writeDebugLog $saveDebugLog;
  194.     }
  195.  
  196.     /**
  197.      * Set temporary storage directory
  198.      *
  199.      * @deprecated
  200.      * @param    string    $pValue        Temporary storage directory
  201.      * @throws    Exception    Exception when directory does not exist
  202.      * @return PHPExcel_Writer_Excel5 
  203.      */
  204.     public function setTempDir($pValue ''{
  205.         return $this;
  206.     }
  207.  
  208.     /**
  209.      * Get Pre-Calculate Formulas
  210.      *
  211.      * @return boolean 
  212.      */
  213.     public function getPreCalculateFormulas({
  214.         return $this->_preCalculateFormulas;
  215.     }
  216.  
  217.     /**
  218.      * Set Pre-Calculate Formulas
  219.      *
  220.      * @param boolean $pValue    Pre-Calculate Formulas?
  221.      */
  222.     public function setPreCalculateFormulas($pValue true{
  223.         $this->_preCalculateFormulas $pValue;
  224.     }
  225.  
  226.     private function _buildWorksheetEschers()
  227.     {
  228.         // 1-based index to BstoreContainer
  229.         $blipIndex 0;
  230.  
  231.         foreach ($this->_phpExcel->getAllsheets(as $sheet{
  232.             // sheet index
  233.             $sheetIndex $sheet->getParent()->getIndex($sheet);
  234.  
  235.             $escher null;
  236.  
  237.             // check if there are any shapes for this sheet
  238.             if (count($sheet->getDrawingCollection()) == 0{
  239.                 continue;
  240.             }
  241.  
  242.             // create intermediate Escher object
  243.             $escher new PHPExcel_Shared_Escher();
  244.  
  245.             // dgContainer
  246.             $dgContainer new PHPExcel_Shared_Escher_DgContainer();
  247.  
  248.             // set the drawing index (we use sheet index + 1)
  249.             $dgId $sheet->getParent()->getIndex($sheet1;
  250.             $dgContainer->setDgId($dgId);
  251.             $escher->setDgContainer($dgContainer);
  252.  
  253.             // spgrContainer
  254.             $spgrContainer new PHPExcel_Shared_Escher_DgContainer_SpgrContainer();
  255.             $dgContainer->setSpgrContainer($spgrContainer);
  256.  
  257.             // add one shape which is the group shape
  258.             $spContainer new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  259.             $spContainer->setSpgr(true);
  260.             $spContainer->setSpType(0);
  261.             $spContainer->setSpId(($sheet->getParent()->getIndex($sheet1<< 10);
  262.             $spgrContainer->addChild($spContainer);
  263.  
  264.             // add the shapes
  265.  
  266.             $countShapes[$sheetIndex0// count number of shapes (minus group shape), in sheet
  267.  
  268.             foreach ($sheet->getDrawingCollection(as $drawing{
  269.                 ++$blipIndex;
  270.  
  271.                 ++$countShapes[$sheetIndex];
  272.  
  273.                 // add the shape
  274.                 $spContainer new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  275.  
  276.                 // set the shape type
  277.                 $spContainer->setSpType(0x004B);
  278.  
  279.                 // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
  280.                 $reducedSpId $countShapes[$sheetIndex];
  281.                 $spId $reducedSpId
  282.                     | ($sheet->getParent()->getIndex($sheet1<< 10;
  283.                 $spContainer->setSpId($spId);
  284.  
  285.                 // keep track of last reducedSpId
  286.                 $lastReducedSpId $reducedSpId;
  287.  
  288.                 // keep track of last spId
  289.                 $lastSpId $spId;
  290.  
  291.                 // set the BLIP index
  292.                 $spContainer->setOPT(0x4104$blipIndex);
  293.  
  294.                 // set coordinates and offsets, client anchor
  295.                 $coordinates $drawing->getCoordinates();
  296.                 $offsetX $drawing->getOffsetX();
  297.                 $offsetY $drawing->getOffsetY();
  298.                 $width $drawing->getWidth();
  299.                 $height $drawing->getHeight();
  300.  
  301.                 $twoAnchor PHPExcel_Shared_Excel5::oneAnchor2twoAnchor($sheet$coordinates$offsetX$offsetY$width$height);
  302.  
  303.                 $spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
  304.                 $spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
  305.                 $spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
  306.                 $spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
  307.                 $spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
  308.                 $spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
  309.  
  310.                 $spgrContainer->addChild($spContainer);
  311.             }
  312.  
  313.             // identifier clusters, used for workbook Escher object
  314.             $this->_IDCLs[$dgId$lastReducedSpId;
  315.  
  316.             // set last shape index
  317.             $dgContainer->setLastSpId($lastSpId);
  318.  
  319.             // set the Escher object
  320.             $this->_writerWorksheets[$sheetIndex]->setEscher($escher);
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * Build the Escher object corresponding to the MSODRAWINGGROUP record
  326.      */
  327.     private function _buildWorkbookEscher()
  328.     {
  329.         $escher null;
  330.  
  331.         // any drawings in this workbook?
  332.         $found false;
  333.         foreach ($this->_phpExcel->getAllSheets(as $sheet{
  334.             if (count($sheet->getDrawingCollection()) 0{
  335.                 $found true;
  336.             }
  337.         }
  338.  
  339.         // nothing to do if there are no drawings
  340.         if (!$found{
  341.             return;
  342.         }
  343.  
  344.         // if we reach here, then there are drawings in the workbook
  345.         $escher new PHPExcel_Shared_Escher();
  346.  
  347.         // dggContainer
  348.         $dggContainer new PHPExcel_Shared_Escher_DggContainer();
  349.         $escher->setDggContainer($dggContainer);
  350.  
  351.         // set IDCLs (identifier clusters)
  352.         $dggContainer->setIDCLs($this->_IDCLs);
  353.  
  354.         // this loop is for determining maximum shape identifier of all drawing
  355.         $spIdMax 0;
  356.         $totalCountShapes 0;
  357.         $countDrawings 0;
  358.  
  359.         foreach ($this->_phpExcel->getAllsheets(as $sheet{
  360.             $sheetCountShapes 0// count number of shapes (minus group shape), in sheet
  361.  
  362.             if (count($sheet->getDrawingCollection()) 0{
  363.                 ++$countDrawings;
  364.  
  365.                 foreach ($sheet->getDrawingCollection(as $drawing{
  366.                     ++$sheetCountShapes;
  367.                     ++$totalCountShapes;
  368.  
  369.                     $spId $sheetCountShapes
  370.                         | ($this->_phpExcel->getIndex($sheet1<< 10;
  371.                     $spIdMax max($spId$spIdMax);
  372.                 }
  373.             }
  374.         }
  375.  
  376.         $dggContainer->setSpIdMax($spIdMax 1);
  377.         $dggContainer->setCDgSaved($countDrawings);
  378.         $dggContainer->setCSpSaved($totalCountShapes $countDrawings)// total number of shapes incl. one group shapes per drawing
  379.  
  380.         // bstoreContainer
  381.         $bstoreContainer new PHPExcel_Shared_Escher_DggContainer_BstoreContainer();
  382.         $dggContainer->setBstoreContainer($bstoreContainer);
  383.  
  384.         // the BSE's (all the images)
  385.         foreach ($this->_phpExcel->getAllsheets(as $sheet{
  386.             foreach ($sheet->getDrawingCollection(as $drawing{
  387.                 if ($drawing instanceof PHPExcel_Worksheet_Drawing{
  388.  
  389.                     $filename $drawing->getPath();
  390.  
  391.                     list($imagesx$imagesy$imageFormatgetimagesize($filename);
  392.  
  393.                     switch ($imageFormat{
  394.  
  395.                     case 1// GIF, not supported by BIFF8, we convert to PNG
  396.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  397.                         $imageResource imagecreatefromgif($filename);
  398.                         ob_start();
  399.                         imagepng($imageResource);
  400.                         $blipData ob_get_contents();
  401.                         ob_end_clean();
  402.                         break;
  403.  
  404.                     case 2// JPEG
  405.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  406.                         $blipData file_get_contents($filename);
  407.                         break;
  408.  
  409.                     case 3// PNG
  410.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  411.                         $blipData file_get_contents($filename);
  412.                         break;
  413.  
  414.                     case 6// Windows DIB (BMP), we convert to PNG
  415.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  416.                         $imageResource PHPExcel_Shared_Drawing::imagecreatefrombmp($filename);
  417.                         ob_start();
  418.                         imagepng($imageResource);
  419.                         $blipData ob_get_contents();
  420.                         ob_end_clean();
  421.                         break;
  422.  
  423.                     defaultcontinue 2;
  424.  
  425.                     }
  426.  
  427.                     $blip new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  428.                     $blip->setData($blipData);
  429.  
  430.                     $BSE new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  431.                     $BSE->setBlipType($blipType);
  432.                     $BSE->setBlip($blip);
  433.  
  434.                     $bstoreContainer->addBSE($BSE);
  435.  
  436.                 else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing{
  437.  
  438.                     switch ($drawing->getRenderingFunction()) {
  439.  
  440.                     case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
  441.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  442.                         $renderingFunction 'imagejpeg';
  443.                         break;
  444.  
  445.                     case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
  446.                     case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
  447.                     case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
  448.                         $blipType PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  449.                         $renderingFunction 'imagepng';
  450.                         break;
  451.  
  452.                     }
  453.  
  454.                     ob_start();
  455.                     call_user_func($renderingFunction$drawing->getImageResource());
  456.                     $blipData ob_get_contents();
  457.                     ob_end_clean();
  458.  
  459.                     $blip new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  460.                     $blip->setData($blipData);
  461.  
  462.                     $BSE new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  463.                     $BSE->setBlipType($blipType);
  464.                     $BSE->setBlip($blip);
  465.  
  466.                     $bstoreContainer->addBSE($BSE);
  467.                 }
  468.             }
  469.         }
  470.  
  471.         // Set the Escher object
  472.         $this->_writerWorkbook->setEscher($escher);
  473.     }
  474.  
  475. }

Documentation generated on Thu, 26 Aug 2010 17:41:56 +0200 by phpDocumentor 1.4.3