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 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_Excel2007
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer_Excel2007
  34.  * @copyright  Copyright (c) 2006 - 2011 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    array();
  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    array();
  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.         $writerPartsArray array(    'stringtable'    => 'PHPExcel_Writer_Excel2007_StringTable',
  140.                                     'contenttypes'    => 'PHPExcel_Writer_Excel2007_ContentTypes',
  141.                                     'docprops'         => 'PHPExcel_Writer_Excel2007_DocProps',
  142.                                     'rels'            => 'PHPExcel_Writer_Excel2007_Rels',
  143.                                     'theme'         => 'PHPExcel_Writer_Excel2007_Theme',
  144.                                     'style'         => 'PHPExcel_Writer_Excel2007_Style',
  145.                                     'workbook'         => 'PHPExcel_Writer_Excel2007_Workbook',
  146.                                     'worksheet'     => 'PHPExcel_Writer_Excel2007_Worksheet',
  147.                                     'drawing'         => 'PHPExcel_Writer_Excel2007_Drawing',
  148.                                     'comments'         => 'PHPExcel_Writer_Excel2007_Comments'
  149.                                  );
  150.  
  151.         //    Initialise writer parts
  152.         //        and Assign their parent IWriters
  153.         foreach ($writerPartsArray as $writer => $class{
  154.             $this->_writerParts[$writernew $class($this);
  155.         }
  156.  
  157.         $hashTablesArray array'_stylesConditionalHashTable',    '_fillHashTable',        '_fontHashTable',
  158.                                   '_bordersHashTable',                '_numFmtHashTable',        '_drawingHashTable'
  159.                                 );
  160.  
  161.         // Set HashTable variables
  162.         foreach ($hashTablesArray as $tableName{
  163.             $this->$tableName     new PHPExcel_HashTable();
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Get writer part
  169.      *
  170.      * @param     string     $pPartName        Writer part name
  171.      * @return     PHPExcel_Writer_Excel2007_WriterPart 
  172.      */
  173.     public function getWriterPart($pPartName ''{
  174.         if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  175.             return $this->_writerParts[strtolower($pPartName)];
  176.         else {
  177.             return null;
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * Save PHPExcel to file
  183.      *
  184.      * @param     string         $pFileName 
  185.      * @throws     Exception
  186.      */
  187.     public function save($pFilename null)
  188.     {
  189.         if ($this->_spreadSheet !== NULL{
  190.             // garbage collect
  191.             $this->_spreadSheet->garbageCollect();
  192.  
  193.             // If $pFilename is php://output or php://stdout, make it a temporary file...
  194.             $originalFilename $pFilename;
  195.             if (strtolower($pFilename== 'php://output' || strtolower($pFilename== 'php://stdout'{
  196.                 $pFilename @tempnam('./''phpxltmp');
  197.                 if ($pFilename == ''{
  198.                     $pFilename $originalFilename;
  199.                 }
  200.             }
  201.  
  202.             $saveDebugLog PHPExcel_Calculation::getInstance()->writeDebugLog;
  203.             PHPExcel_Calculation::getInstance()->writeDebugLog false;
  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.             $zipClass PHPExcel_Settings::getZipClass();
  224.             $objZip new $zipClass();
  225.  
  226.             if (file_exists($pFilename)) {
  227.                 unlink($pFilename);
  228.             }
  229.             // Try opening the ZIP file
  230.             if ($objZip->open($pFilenameZIPARCHIVE::OVERWRITE!== true{
  231.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  232.                     throw new Exception("Could not open " $pFilename " for writing.");
  233.                 }
  234.             }
  235.  
  236.             // Add [Content_Types].xml to ZIP file
  237.             $objZip->addFromString('[Content_Types].xml',             $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
  238.  
  239.             // Add relationships to ZIP file
  240.             $objZip->addFromString('_rels/.rels',                     $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  241.             $objZip->addFromString('xl/_rels/workbook.xml.rels',     $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  242.  
  243.             // Add document properties to ZIP file
  244.             $objZip->addFromString('docProps/app.xml',                 $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  245.             $objZip->addFromString('docProps/core.xml',             $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  246.             $customPropertiesPart $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
  247.             if ($customPropertiesPart !== NULL{
  248.                 $objZip->addFromString('docProps/custom.xml',         $customPropertiesPart);
  249.             }
  250.  
  251.             // Add theme to ZIP file
  252.             $objZip->addFromString('xl/theme/theme1.xml',             $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  253.  
  254.             // Add string table to ZIP file
  255.             $objZip->addFromString('xl/sharedStrings.xml',             $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  256.  
  257.             // Add styles to ZIP file
  258.             $objZip->addFromString('xl/styles.xml',                 $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  259.  
  260.             // Add workbook to ZIP file
  261.             $objZip->addFromString('xl/workbook.xml',                 $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
  262.  
  263.             // Add worksheets
  264.             for ($i 0$i $this->_spreadSheet->getSheetCount()++$i{
  265.                 $objZip->addFromString('xl/worksheets/sheet' ($i 1'.xml'$this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i)$this->_stringTable));
  266.             }
  267.  
  268.             // Add worksheet relationships (drawings, ...)
  269.             for ($i 0$i $this->_spreadSheet->getSheetCount()++$i{
  270.  
  271.                 // Add relationships
  272.                 $objZip->addFromString('xl/worksheets/_rels/sheet' ($i 1'.xml.rels',     $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i)($i 1)));
  273.  
  274.                 // Add drawing relationship parts
  275.                 if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count(0{
  276.                     // Drawing relationships
  277.                     $objZip->addFromString('xl/drawings/_rels/drawing' ($i 1'.xml.rels'$this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
  278.  
  279.                     // Drawings
  280.                     $objZip->addFromString('xl/drawings/drawing' ($i 1'.xml'$this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
  281.                 }
  282.  
  283.                 // Add comment relationship parts
  284.                 if (count($this->_spreadSheet->getSheet($i)->getComments()) 0{
  285.                     // VML Comments
  286.                     $objZip->addFromString('xl/drawings/vmlDrawing' ($i 1'.vml'$this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
  287.  
  288.                     // Comments
  289.                     $objZip->addFromString('xl/comments' ($i 1'.xml'$this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
  290.                 }
  291.  
  292.                 // Add header/footer relationship parts
  293.                 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) 0{
  294.                     // VML Drawings
  295.                     $objZip->addFromString('xl/drawings/vmlDrawingHF' ($i 1'.vml'$this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
  296.  
  297.                     // VML Drawing relationships
  298.                     $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' ($i 1'.vml.rels'$this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
  299.  
  300.                     // Media
  301.                     foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages(as $image{
  302.                         $objZip->addFromString('xl/media/' $image->getIndexedFilename()file_get_contents($image->getPath()));
  303.                     }
  304.                 }
  305.             }
  306.  
  307.             // Add media
  308.             for ($i 0$i $this->getDrawingHashTable()->count()++$i{
  309.                 if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPExcel_Worksheet_Drawing{
  310.                     $imageContents null;
  311.                     $imagePath $this->getDrawingHashTable()->getByIndex($i)->getPath();
  312.  
  313.                     if (strpos($imagePath'zip://'!== false{
  314.                         $imagePath substr($imagePath6);
  315.                         $imagePathSplitted explode('#'$imagePath);
  316.  
  317.                         $imageZip new ZipArchive();
  318.                         $imageZip->open($imagePathSplitted[0]);
  319.                         $imageContents $imageZip->getFromName($imagePathSplitted[1]);
  320.                         $imageZip->close();
  321.                         unset($imageZip);
  322.                     else {
  323.                         $imageContents file_get_contents($imagePath);
  324.                     }
  325.  
  326.                     $objZip->addFromString('xl/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  327.                 else if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPExcel_Worksheet_MemoryDrawing{
  328.                     ob_start();
  329.                     call_user_func(
  330.                         $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  331.                         $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  332.                     );
  333.                     $imageContents ob_get_contents();
  334.                     ob_end_clean();
  335.  
  336.                     $objZip->addFromString('xl/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  337.                 }
  338.             }
  339.  
  340.             PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  341.             PHPExcel_Calculation::getInstance()->writeDebugLog $saveDebugLog;
  342.  
  343.             // Close file
  344.             if ($objZip->close(=== false{
  345.                 throw new Exception("Could not close zip file $pFilename.");
  346.             }
  347.  
  348.             // If a temporary file was used, copy it to the correct file stream
  349.             if ($originalFilename != $pFilename{
  350.                 if (copy($pFilename$originalFilename=== false{
  351.                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  352.                 }
  353.                 @unlink($pFilename);
  354.             }
  355.         else {
  356.             throw new Exception("PHPExcel object unassigned.");
  357.         }
  358.     }
  359.  
  360.     /**
  361.      * Get PHPExcel object
  362.      *
  363.      * @return PHPExcel 
  364.      * @throws Exception
  365.      */
  366.     public function getPHPExcel({
  367.         if ($this->_spreadSheet !== null{
  368.             return $this->_spreadSheet;
  369.         else {
  370.             throw new Exception("No PHPExcel assigned.");
  371.         }
  372.     }
  373.  
  374.     /**
  375.      * Set PHPExcel object
  376.      *
  377.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  378.      * @throws    Exception
  379.      * @return PHPExcel_Writer_Excel2007 
  380.      */
  381.     public function setPHPExcel(PHPExcel $pPHPExcel null{
  382.         $this->_spreadSheet $pPHPExcel;
  383.         return $this;
  384.     }
  385.  
  386.     /**
  387.      * Get string table
  388.      *
  389.      * @return string[] 
  390.      */
  391.     public function getStringTable({
  392.         return $this->_stringTable;
  393.     }
  394.  
  395.     /**
  396.      * Get PHPExcel_Style_Conditional HashTable
  397.      *
  398.      * @return PHPExcel_HashTable 
  399.      */
  400.     public function getStylesConditionalHashTable({
  401.         return $this->_stylesConditionalHashTable;
  402.     }
  403.  
  404.     /**
  405.      * Get PHPExcel_Style_Fill HashTable
  406.      *
  407.      * @return PHPExcel_HashTable 
  408.      */
  409.     public function getFillHashTable({
  410.         return $this->_fillHashTable;
  411.     }
  412.  
  413.     /**
  414.      * Get PHPExcel_Style_Font HashTable
  415.      *
  416.      * @return PHPExcel_HashTable 
  417.      */
  418.     public function getFontHashTable({
  419.         return $this->_fontHashTable;
  420.     }
  421.  
  422.     /**
  423.      * Get PHPExcel_Style_Borders HashTable
  424.      *
  425.      * @return PHPExcel_HashTable 
  426.      */
  427.     public function getBordersHashTable({
  428.         return $this->_bordersHashTable;
  429.     }
  430.  
  431.     /**
  432.      * Get PHPExcel_Style_NumberFormat HashTable
  433.      *
  434.      * @return PHPExcel_HashTable 
  435.      */
  436.     public function getNumFmtHashTable({
  437.         return $this->_numFmtHashTable;
  438.     }
  439.  
  440.     /**
  441.      * Get PHPExcel_Worksheet_BaseDrawing HashTable
  442.      *
  443.      * @return PHPExcel_HashTable 
  444.      */
  445.     public function getDrawingHashTable({
  446.         return $this->_drawingHashTable;
  447.     }
  448.  
  449.     /**
  450.      * Get Pre-Calculate Formulas
  451.      *
  452.      * @return boolean 
  453.      */
  454.     public function getPreCalculateFormulas({
  455.         return $this->_preCalculateFormulas;
  456.     }
  457.  
  458.     /**
  459.      * Set Pre-Calculate Formulas
  460.      *
  461.      * @param boolean $pValue    Pre-Calculate Formulas?
  462.      */
  463.     public function setPreCalculateFormulas($pValue true{
  464.         $this->_preCalculateFormulas $pValue;
  465.     }
  466.  
  467.     /**
  468.      * Get Office2003 compatibility
  469.      *
  470.      * @return boolean 
  471.      */
  472.     public function getOffice2003Compatibility({
  473.         return $this->_office2003compatibility;
  474.     }
  475.  
  476.     /**
  477.      * Set Pre-Calculate Formulas
  478.      *
  479.      * @param boolean $pValue    Office2003 compatibility?
  480.      * @return PHPExcel_Writer_Excel2007 
  481.      */
  482.     public function setOffice2003Compatibility($pValue false{
  483.         $this->_office2003compatibility $pValue;
  484.         return $this;
  485.     }
  486.  
  487.     /**
  488.      * Get use disk caching where possible?
  489.      *
  490.      * @return boolean 
  491.      */
  492.     public function getUseDiskCaching({
  493.         return $this->_useDiskCaching;
  494.     }
  495.  
  496.     /**
  497.      * Set use disk caching where possible?
  498.      *
  499.      * @param     boolean     $pValue 
  500.      * @param    string        $pDirectory        Disk caching directory
  501.      * @throws    Exception    Exception when directory does not exist
  502.      * @return PHPExcel_Writer_Excel2007 
  503.      */
  504.     public function setUseDiskCaching($pValue false$pDirectory null{
  505.         $this->_useDiskCaching $pValue;
  506.  
  507.         if ($pDirectory !== NULL{
  508.             if (is_dir($pDirectory)) {
  509.                 $this->_diskCachingDirectory $pDirectory;
  510.             else {
  511.                 throw new Exception("Directory does not exist: $pDirectory");
  512.             }
  513.         }
  514.         return $this;
  515.     }
  516.  
  517.     /**
  518.      * Get disk caching directory
  519.      *
  520.      * @return string 
  521.      */
  522.     public function getDiskCachingDirectory({
  523.         return $this->_diskCachingDirectory;
  524.     }
  525. }

Documentation generated on Sun, 27 Feb 2011 16:31:29 -0800 by phpDocumentor 1.4.3