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

Documentation generated on Mon, 25 Aug 2008 13:49:24 +0200 by phpDocumentor 1.4.1