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

Source for file Serialized.php

Documentation is available at Serialized.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
  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.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_Serialized
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Writer_Serialized implements PHPExcel_Writer_IWriter
  37. {
  38.     /**
  39.      * Private PHPExcel
  40.      *
  41.      * @var PHPExcel 
  42.      */
  43.     private $_spreadSheet;
  44.  
  45.     /**
  46.      * Create a new PHPExcel_Writer_Serialized
  47.      *
  48.      * @param     PHPExcel    $pPHPExcel 
  49.      */
  50.     public function __construct(PHPExcel $pPHPExcel null)
  51.     {
  52.         // Assign PHPExcel
  53.         $this->setPHPExcel($pPHPExcel);
  54.     }
  55.  
  56.     /**
  57.      * Save PHPExcel to file
  58.      *
  59.      * @param     string         $pFileName 
  60.      * @throws     Exception
  61.      */
  62.     public function save($pFilename null)
  63.     {
  64.         if (!is_null($this->_spreadSheet)) {
  65.             // Garbage collect
  66.             $this->_spreadSheet->garbageCollect();
  67.  
  68.             // Garbage collect...
  69.             foreach ($this->_spreadSheet->getAllSheets(as $sheet{
  70.                 $sheet->garbageCollect();
  71.             }
  72.  
  73.             // Create new ZIP file and open it for writing
  74.             $objZip new ZipArchive();
  75.  
  76.             // Try opening the ZIP file
  77.             if ($objZip->open($pFilenameZIPARCHIVE::OVERWRITE!== true{
  78.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  79.                     throw new Exception("Could not open " $pFilename " for writing.");
  80.                 }
  81.             }
  82.  
  83.             // Add media
  84.             $sheetCount $this->_spreadSheet->getSheetCount();
  85.             for ($i 0$i $sheetCount++$i{
  86.                 for ($j 0$j $this->_spreadSheet->getSheet($i)->getDrawingCollection()->count()++$j{
  87.                     if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcel_Worksheet_BaseDrawing{
  88.                         $imgTemp $this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j);
  89.                         $objZip->addFromString('media/' $imgTemp->getFilename()file_get_contents($imgTemp->getPath()));
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             // Add phpexcel.xml to the document, which represents a PHP serialized PHPExcel object
  95.             $objZip->addFromString('phpexcel.xml'$this->_writeSerialized($this->_spreadSheet$pFilename));
  96.  
  97.             // Close file
  98.             if ($objZip->close(=== false{
  99.                 throw new Exception("Could not close zip file $pFilename.");
  100.             }
  101.         else {
  102.             throw new Exception("PHPExcel object unassigned.");
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Get PHPExcel object
  108.      *
  109.      * @return PHPExcel 
  110.      * @throws Exception
  111.      */
  112.     public function getPHPExcel({
  113.         if (!is_null($this->_spreadSheet)) {
  114.             return $this->_spreadSheet;
  115.         else {
  116.             throw new Exception("No PHPExcel assigned.");
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Get PHPExcel object
  122.      *
  123.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  124.      * @throws    Exception
  125.      * @return PHPExcel_Writer_Serialized 
  126.      */
  127.     public function setPHPExcel(PHPExcel $pPHPExcel null{
  128.         $this->_spreadSheet $pPHPExcel;
  129.         return $this;
  130.     }
  131.  
  132.     /**
  133.      * Serialize PHPExcel object to XML
  134.      *
  135.      * @param     PHPExcel    $pPHPExcel 
  136.      * @param     string        $pFilename 
  137.      * @return     string         XML Output
  138.      * @throws     Exception
  139.      */
  140.     private function _writeSerialized(PHPExcel $pPHPExcel null$pFilename '')
  141.     {
  142.         // Clone $pPHPExcel
  143.         $pPHPExcel clone $pPHPExcel;
  144.  
  145.         // Update media links
  146.         $sheetCount $pPHPExcel->getSheetCount();
  147.         for ($i 0$i $sheetCount++$i{
  148.             for ($j 0$j $pPHPExcel->getSheet($i)->getDrawingCollection()->count()++$j{
  149.                 if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcel_Worksheet_BaseDrawing{
  150.                     $imgTemp =$pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  151.                     $imgTemp->setPath('zip://' $pFilename '#media/' $imgTemp->getFilename()false);
  152.                 }
  153.             }
  154.         }
  155.  
  156.         // Create XML writer
  157.         $objWriter new xmlWriter();
  158.         $objWriter->openMemory();
  159.         $objWriter->setIndent(true);
  160.  
  161.         // XML header
  162.         $objWriter->startDocument('1.0','UTF-8','yes');
  163.  
  164.         // PHPExcel
  165.         $objWriter->startElement('PHPExcel');
  166.         $objWriter->writeAttribute('version''1.7.3c');
  167.  
  168.             // Comment
  169.             $objWriter->writeComment('This file has been generated using PHPExcel v1.7.3c (http://www.codeplex.com/PHPExcel). It contains a base64 encoded serialized version of the PHPExcel internal object.');
  170.  
  171.             // Data
  172.             $objWriter->startElement('data');
  173.                 $objWriter->writeCDatabase64_encode(serialize($pPHPExcel)) );
  174.             $objWriter->endElement();
  175.  
  176.         $objWriter->endElement();
  177.  
  178.         // Return
  179.         return $objWriter->outputMemory(true);
  180.     }
  181. }

Documentation generated on Tue, 01 Jun 2010 17:05:57 +0200 by phpDocumentor 1.4.3