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

Documentation generated on Mon, 11 Jan 2010 08:13:39 +0100 by phpDocumentor 1.4.1