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 - 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
  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 */
  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 - 2009 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($pFilenameZIPARCHIVE::OVERWRITE!== true{
  94.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  95.                     throw new Exception("Could not open " $pFilename " for writing.");
  96.                 }
  97.             }
  98.  
  99.             // Add media
  100.             $sheetCount $this->_spreadSheet->getSheetCount();
  101.             for ($i 0$i $sheetCount++$i{
  102.                 for ($j 0$j $this->_spreadSheet->getSheet($i)->getDrawingCollection()->count()++$j{
  103.                     if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcel_Worksheet_BaseDrawing{
  104.                         $imgTemp $this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j);
  105.                         $objZip->addFromString('media/' $imgTemp->getFilename()file_get_contents($imgTemp->getPath()));
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             // Add phpexcel.xml to the document, which represents a PHP serialized PHPExcel object
  111.             $objZip->addFromString('phpexcel.xml'$this->_writeSerialized($this->_spreadSheet$pFilename));
  112.  
  113.             // Close file
  114.             if ($objZip->close(=== false{
  115.                 throw new Exception("Could not close zip file $pFilename.");
  116.             }
  117.         else {
  118.             throw new Exception("PHPExcel object unassigned.");
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * Get PHPExcel object
  124.      *
  125.      * @return PHPExcel 
  126.      * @throws Exception
  127.      */
  128.     public function getPHPExcel({
  129.         if (!is_null($this->_spreadSheet)) {
  130.             return $this->_spreadSheet;
  131.         else {
  132.             throw new Exception("No PHPExcel assigned.");
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Get PHPExcel object
  138.      *
  139.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  140.      * @throws    Exception
  141.      */
  142.     public function setPHPExcel(PHPExcel $pPHPExcel null{
  143.         $this->_spreadSheet = $pPHPExcel;
  144.     }
  145.  
  146.     /**
  147.      * Serialize PHPExcel object to XML
  148.      *
  149.      * @param     PHPExcel    $pPHPExcel 
  150.      * @param     string        $pFilename 
  151.      * @return     string         XML Output
  152.      * @throws     Exception
  153.      */
  154.     private function _writeSerialized(PHPExcel $pPHPExcel null$pFilename '')
  155.     {
  156.         // Clone $pPHPExcel
  157.         $pPHPExcel clone $pPHPExcel;
  158.  
  159.         // Update media links
  160.         $sheetCount $pPHPExcel->getSheetCount();
  161.         for ($i 0$i $sheetCount++$i{
  162.             for ($j 0$j $pPHPExcel->getSheet($i)->getDrawingCollection()->count()++$j{
  163.                 if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcel_Worksheet_BaseDrawing{
  164.                     $imgTemp =$pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  165.                     $imgTemp->setPath('zip://' $pFilename '#media/' $imgTemp->getFilename()false);
  166.                 }
  167.             }
  168.         }
  169.  
  170.         // Create XML writer
  171.         $objWriter new xmlWriter();
  172.         $objWriter->openMemory();
  173.         $objWriter->setIndent(true);
  174.  
  175.         // XML header
  176.         $objWriter->startDocument('1.0','UTF-8','yes');
  177.  
  178.         // PHPExcel
  179.         $objWriter->startElement('PHPExcel');
  180.         $objWriter->writeAttribute('version''1.6.5');
  181.  
  182.             // Comment
  183.             $objWriter->writeComment('This file has been generated using PHPExcel v1.6.5 (http://www.codeplex.com/PHPExcel). It contains a base64 encoded serialized version of the PHPExcel internal object.');
  184.  
  185.             // Data
  186.             $objWriter->startElement('data');
  187.                 $objWriter->writeCDatabase64_encode(serialize($pPHPExcel)) );
  188.             $objWriter->endElement();
  189.  
  190.         $objWriter->endElement();
  191.  
  192.         // Return
  193.         return $objWriter->outputMemory(true);
  194.     }
  195. }

Documentation generated on Mon, 05 Jan 2009 20:38:28 +0100 by phpDocumentor 1.4.1