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

Source for file Excel2007.php

Documentation is available at Excel2007.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_Excel2007
  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_Excel2007
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer_Excel2007
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Writer_Excel2007 implements PHPExcel_Writer_IWriter
  37. {
  38.     /**
  39.      * Pre-calculate formulas
  40.      *
  41.      * @var boolean 
  42.      */
  43.     private $_preCalculateFormulas true;
  44.  
  45.     /**
  46.      * Office2003 compatibility
  47.      *
  48.      * @var boolean 
  49.      */
  50.     private $_office2003compatibility false;
  51.  
  52.     /**
  53.      * Private writer parts
  54.      *
  55.      * @var PHPExcel_Writer_Excel2007_WriterPart[] 
  56.      */
  57.     private $_writerParts;
  58.  
  59.     /**
  60.      * Private PHPExcel
  61.      *
  62.      * @var PHPExcel 
  63.      */
  64.     private $_spreadSheet;
  65.  
  66.     /**
  67.      * Private string table
  68.      *
  69.      * @var string[] 
  70.      */
  71.     private $_stringTable;
  72.  
  73.     /**
  74.      * Private unique PHPExcel_Style_Conditional HashTable
  75.      *
  76.      * @var PHPExcel_HashTable 
  77.      */
  78.     private $_stylesConditionalHashTable;
  79.  
  80.     /**
  81.      * Private unique PHPExcel_Style_Fill HashTable
  82.      *
  83.      * @var PHPExcel_HashTable 
  84.      */
  85.     private $_fillHashTable;
  86.  
  87.     /**
  88.      * Private unique PHPExcel_Style_Font HashTable
  89.      *
  90.      * @var PHPExcel_HashTable 
  91.      */
  92.     private $_fontHashTable;
  93.  
  94.     /**
  95.      * Private unique PHPExcel_Style_Borders HashTable
  96.      *
  97.      * @var PHPExcel_HashTable 
  98.      */
  99.     private $_bordersHashTable ;
  100.  
  101.     /**
  102.      * Private unique PHPExcel_Style_NumberFormat HashTable
  103.      *
  104.      * @var PHPExcel_HashTable 
  105.      */
  106.     private $_numFmtHashTable;
  107.  
  108.     /**
  109.      * Private unique PHPExcel_Worksheet_BaseDrawing HashTable
  110.      *
  111.      * @var PHPExcel_HashTable 
  112.      */
  113.     private $_drawingHashTable;
  114.  
  115.     /**
  116.      * Use disk caching where possible?
  117.      *
  118.      * @var boolean 
  119.      */
  120.     private $_useDiskCaching false;
  121.  
  122.     /**
  123.      * Disk caching directory
  124.      *
  125.      * @var string 
  126.      */
  127.     private $_diskCachingDirectory;
  128.  
  129.     /**
  130.      * Create a new PHPExcel_Writer_Excel2007
  131.      *
  132.      * @param     PHPExcel    $pPHPExcel 
  133.      */
  134.     public function __construct(PHPExcel $pPHPExcel null)
  135.     {
  136.         // Assign PHPExcel
  137.         $this->setPHPExcel($pPHPExcel);
  138.  
  139.         // Set up disk caching location
  140.         $this->_diskCachingDirectory './';
  141.  
  142.         // Initialise writer parts
  143.         $this->_writerParts['stringtable']        new PHPExcel_Writer_Excel2007_StringTable();
  144.         $this->_writerParts['contenttypes']     new PHPExcel_Writer_Excel2007_ContentTypes();
  145.         $this->_writerParts['docprops']         new PHPExcel_Writer_Excel2007_DocProps();
  146.         $this->_writerParts['rels']             new PHPExcel_Writer_Excel2007_Rels();
  147.         $this->_writerParts['theme']             new PHPExcel_Writer_Excel2007_Theme();
  148.         $this->_writerParts['style']             new PHPExcel_Writer_Excel2007_Style();
  149.         $this->_writerParts['workbook']         new PHPExcel_Writer_Excel2007_Workbook();
  150.         $this->_writerParts['worksheet']         new PHPExcel_Writer_Excel2007_Worksheet();
  151.         $this->_writerParts['drawing']             new PHPExcel_Writer_Excel2007_Drawing();
  152.         $this->_writerParts['comments']         new PHPExcel_Writer_Excel2007_Comments();
  153.  
  154.         // Assign parent IWriter
  155.         foreach ($this->_writerParts as $writer{
  156.             $writer->setParentWriter($this);
  157.         }
  158.  
  159.         // Set HashTable variables
  160.         $this->_stringTable                    array();
  161.         $this->_stylesConditionalHashTable     new PHPExcel_HashTable();
  162.         $this->_fillHashTable                 new PHPExcel_HashTable();
  163.         $this->_fontHashTable                 new PHPExcel_HashTable();
  164.         $this->_bordersHashTable             new PHPExcel_HashTable();
  165.         $this->_numFmtHashTable             new PHPExcel_HashTable();
  166.         $this->_drawingHashTable             new PHPExcel_HashTable();
  167.     }
  168.  
  169.     /**
  170.      * Get writer part
  171.      *
  172.      * @param     string     $pPartName        Writer part name
  173.      * @return     PHPExcel_Writer_Excel2007_WriterPart 
  174.      */
  175.     function getWriterPart($pPartName ''{
  176.         if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  177.             return $this->_writerParts[strtolower($pPartName)];
  178.         else {
  179.             return null;
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Save PHPExcel to file
  185.      *
  186.      * @param     string         $pFileName 
  187.      * @throws     Exception
  188.      */
  189.     public function save($pFilename null)
  190.     {
  191.         if (!is_null($this->_spreadSheet)) {
  192.             // garbage collect
  193.             $this->_spreadSheet->garbageCollect();
  194.  
  195.             // If $pFilename is php://output or php://stdout, make it a temporary file...
  196.             $originalFilename $pFilename;
  197.             if (strtolower($pFilename== 'php://output' || strtolower($pFilename== 'php://stdout'{
  198.                 $pFilename @tempnam('./''phpxltmp');
  199.                 if ($pFilename == ''{
  200.                     $pFilename $originalFilename;
  201.                 }
  202.             }
  203.  
  204.             $saveDateReturnType PHPExcel_Calculation_Functions::getReturnDateType();
  205.  
  206.             // Create string lookup table
  207.             $this->_stringTable array();
  208.             for ($i 0$i $this->_spreadSheet->getSheetCount()++$i{
  209.                 $this->_stringTable $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i)$this->_stringTable);
  210.             }
  211.  
  212.             // Create styles dictionaries
  213.             $this->_stylesConditionalHashTable->addFromSource(     $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet)             );
  214.             $this->_fillHashTable->addFromSource(                 $this->getWriterPart('Style')->allFills($this->_spreadSheet)             );
  215.             $this->_fontHashTable->addFromSource(                 $this->getWriterPart('Style')->allFonts($this->_spreadSheet)             );
  216.             $this->_bordersHashTable->addFromSource(             $this->getWriterPart('Style')->allBorders($this->_spreadSheet)             );
  217.             $this->_numFmtHashTable->addFromSource(             $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet)     );
  218.  
  219.             // Create drawing dictionary
  220.             $this->_drawingHashTable->addFromSource(             $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet)         );
  221.  
  222.             // Create new ZIP file and open it for writing
  223.             $objZip new ZipArchive();
  224.  
  225.             // Try opening the ZIP file
  226.             if ($objZip->open($pFilenameZIPARCHIVE::OVERWRITE!== true{
  227.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  228.                     throw new Exception("Could not open " $pFilename " for writing.");
  229.                 }
  230.             }
  231.  
  232.             // Add [Content_Types].xml to ZIP file
  233.             $objZip->addFromString('[Content_Types].xml',             $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
  234.  
  235.             // Add relationships to ZIP file
  236.             $objZip->addFromString('_rels/.rels',                     $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  237.             $objZip->addFromString('xl/_rels/workbook.xml.rels',     $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  238.  
  239.             // Add document properties to ZIP file
  240.             $objZip->addFromString('docProps/app.xml',                 $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  241.             $objZip->addFromString('docProps/core.xml',             $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  242.  
  243.             // Add theme to ZIP file
  244.             $objZip->addFromString('xl/theme/theme1.xml',             $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  245.  
  246.             // Add string table to ZIP file
  247.             $objZip->addFromString('xl/sharedStrings.xml',             $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  248.  
  249.             // Add styles to ZIP file
  250.             $objZip->addFromString('xl/styles.xml',                 $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  251.  
  252.             // Add workbook to ZIP file
  253.             $objZip->addFromString('xl/workbook.xml',                 $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
  254.  
  255.             // Add worksheets
  256.             for ($i 0$i $this->_spreadSheet->getSheetCount()++$i{
  257.                 $objZip->addFromString('xl/worksheets/sheet' ($i 1'.xml'$this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i)$this->_stringTable));
  258.             }
  259.  
  260.             // Add worksheet relationships (drawings, ...)
  261.             for ($i 0$i $this->_spreadSheet->getSheetCount()++$i{
  262.  
  263.                 // Add relationships
  264.                 $objZip->addFromString('xl/worksheets/_rels/sheet' ($i 1'.xml.rels',     $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i)($i 1)));
  265.  
  266.                 // Add drawing relationship parts
  267.                 if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count(0{
  268.                     // Drawing relationships
  269.                     $objZip->addFromString('xl/drawings/_rels/drawing' ($i 1'.xml.rels'$this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
  270.  
  271.                     // Drawings
  272.                     $objZip->addFromString('xl/drawings/drawing' ($i 1'.xml'$this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
  273.                 }
  274.  
  275.                 // Add comment relationship parts
  276.                 if (count($this->_spreadSheet->getSheet($i)->getComments()) 0{
  277.                     // VML Comments
  278.                     $objZip->addFromString('xl/drawings/vmlDrawing' ($i 1'.vml'$this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
  279.  
  280.                     // Comments
  281.                     $objZip->addFromString('xl/comments' ($i 1'.xml'$this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
  282.                 }
  283.  
  284.                 // Add header/footer relationship parts
  285.                 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) 0{
  286.                     // VML Drawings
  287.                     $objZip->addFromString('xl/drawings/vmlDrawingHF' ($i 1'.vml'$this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
  288.  
  289.                     // VML Drawing relationships
  290.                     $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' ($i 1'.vml.rels'$this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
  291.  
  292.                     // Media
  293.                     foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages(as $image{
  294.                         $objZip->addFromString('xl/media/' $image->getIndexedFilename()file_get_contents($image->getPath()));
  295.                     }
  296.                 }
  297.             }
  298.  
  299.             // Add media
  300.             for ($i 0$i $this->getDrawingHashTable()->count()++$i{
  301.                 if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPExcel_Worksheet_Drawing{
  302.                     $imageContents null;
  303.                     $imagePath $this->getDrawingHashTable()->getByIndex($i)->getPath();
  304.  
  305.                     if (strpos($imagePath'zip://'!== false{
  306.                         $imagePath substr($imagePath6);
  307.                         $imagePathSplitted explode('#'$imagePath);
  308.  
  309.                         $imageZip new ZipArchive();
  310.                         $imageZip->open($imagePathSplitted[0]);
  311.                         $imageContents $imageZip->getFromName($imagePathSplitted[1]);
  312.                         $imageZip->close();
  313.                         unset($imageZip);
  314.                     else {
  315.                         $imageContents file_get_contents($imagePath);
  316.                     }
  317.  
  318.                     $objZip->addFromString('xl/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  319.                 else if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPExcel_Worksheet_MemoryDrawing{
  320.                     ob_start();
  321.                     call_user_func(
  322.                         $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  323.                         $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  324.                     );
  325.                     $imageContents ob_get_contents();
  326.                     ob_end_clean();
  327.  
  328.                     $objZip->addFromString('xl/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  329.                 }
  330.             }
  331.  
  332.             PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  333.  
  334.             // Close file
  335.             if ($objZip->close(=== false{
  336.                 throw new Exception("Could not close zip file $pFilename.");
  337.             }
  338.  
  339.             // If a temporary file was used, copy it to the correct file stream
  340.             if ($originalFilename != $pFilename{
  341.                 if (copy($pFilename$originalFilename=== false{
  342.                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  343.                 }
  344.                 @unlink($pFilename);
  345.             }
  346.         else {
  347.             throw new Exception("PHPExcel object unassigned.");
  348.         }
  349.     }
  350.  
  351.     /**
  352.      * Get PHPExcel object
  353.      *
  354.      * @return PHPExcel 
  355.      * @throws Exception
  356.      */
  357.     public function getPHPExcel({
  358.         if (!is_null($this->_spreadSheet)) {
  359.             return $this->_spreadSheet;
  360.         else {
  361.             throw new Exception("No PHPExcel assigned.");
  362.         }
  363.     }
  364.  
  365.     /**
  366.      * Set PHPExcel object
  367.      *
  368.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  369.      * @throws    Exception
  370.      * @return PHPExcel_Writer_Excel2007 
  371.      */
  372.     public function setPHPExcel(PHPExcel $pPHPExcel null{
  373.         $this->_spreadSheet $pPHPExcel;
  374.         return $this;
  375.     }
  376.  
  377.     /**
  378.      * Get string table
  379.      *
  380.      * @return string[] 
  381.      */
  382.     public function getStringTable({
  383.         return $this->_stringTable;
  384.     }
  385.  
  386.     /**
  387.      * Get PHPExcel_Style_Conditional HashTable
  388.      *
  389.      * @return PHPExcel_HashTable 
  390.      */
  391.     public function getStylesConditionalHashTable({
  392.         return $this->_stylesConditionalHashTable;
  393.     }
  394.  
  395.     /**
  396.      * Get PHPExcel_Style_Fill HashTable
  397.      *
  398.      * @return PHPExcel_HashTable 
  399.      */
  400.     public function getFillHashTable({
  401.         return $this->_fillHashTable;
  402.     }
  403.  
  404.     /**
  405.      * Get PHPExcel_Style_Font HashTable
  406.      *
  407.      * @return PHPExcel_HashTable 
  408.      */
  409.     public function getFontHashTable({
  410.         return $this->_fontHashTable;
  411.     }
  412.  
  413.     /**
  414.      * Get PHPExcel_Style_Borders HashTable
  415.      *
  416.      * @return PHPExcel_HashTable 
  417.      */
  418.     public function getBordersHashTable({
  419.         return $this->_bordersHashTable;
  420.     }
  421.  
  422.     /**
  423.      * Get PHPExcel_Style_NumberFormat HashTable
  424.      *
  425.      * @return PHPExcel_HashTable 
  426.      */
  427.     public function getNumFmtHashTable({
  428.         return $this->_numFmtHashTable;
  429.     }
  430.  
  431.     /**
  432.      * Get PHPExcel_Worksheet_BaseDrawing HashTable
  433.      *
  434.      * @return PHPExcel_HashTable 
  435.      */
  436.     public function getDrawingHashTable({
  437.         return $this->_drawingHashTable;
  438.     }
  439.  
  440.     /**
  441.      * Get Pre-Calculate Formulas
  442.      *
  443.      * @return boolean 
  444.      */
  445.     public function getPreCalculateFormulas({
  446.         return $this->_preCalculateFormulas;
  447.     }
  448.  
  449.     /**
  450.      * Set Pre-Calculate Formulas
  451.      *
  452.      * @param boolean $pValue    Pre-Calculate Formulas?
  453.      */
  454.     public function setPreCalculateFormulas($pValue true{
  455.         $this->_preCalculateFormulas $pValue;
  456.     }
  457.  
  458.     /**
  459.      * Get Office2003 compatibility
  460.      *
  461.      * @return boolean 
  462.      */
  463.     public function getOffice2003Compatibility({
  464.         return $this->_office2003compatibility;
  465.     }
  466.  
  467.     /**
  468.      * Set Pre-Calculate Formulas
  469.      *
  470.      * @param boolean $pValue    Office2003 compatibility?
  471.      * @return PHPExcel_Writer_Excel2007 
  472.      */
  473.     public function setOffice2003Compatibility($pValue false{
  474.         $this->_office2003compatibility $pValue;
  475.         return $this;
  476.     }
  477.  
  478.     /**
  479.      * Get use disk caching where possible?
  480.      *
  481.      * @return boolean 
  482.      */
  483.     public function getUseDiskCaching({
  484.         return $this->_useDiskCaching;
  485.     }
  486.  
  487.     /**
  488.      * Set use disk caching where possible?
  489.      *
  490.      * @param     boolean     $pValue 
  491.      * @param    string        $pDirectory        Disk caching directory
  492.      * @throws    Exception    Exception when directory does not exist
  493.      * @return PHPExcel_Writer_Excel2007 
  494.      */
  495.     public function setUseDiskCaching($pValue false$pDirectory null{
  496.         $this->_useDiskCaching $pValue;
  497.  
  498.         if (!is_null($pDirectory)) {
  499.             if (is_dir($pDirectory)) {
  500.                 $this->_diskCachingDirectory $pDirectory;
  501.             else {
  502.                 throw new Exception("Directory does not exist: $pDirectory");
  503.             }
  504.         }
  505.         return $this;
  506.     }
  507.  
  508.     /**
  509.      * Get disk caching directory
  510.      *
  511.      * @return string 
  512.      */
  513.     public function getDiskCachingDirectory({
  514.         return $this->_diskCachingDirectory;
  515.     }
  516. }

Documentation generated on Tue, 01 Jun 2010 17:03:45 +0200 by phpDocumentor 1.4.3