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.4, 2010-08-26
  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.             $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
  205.             PHPExcel_Calculation::getInstance()->writeDebugLog = false;
  206.             $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  207.  
  208.             // Create string lookup table
  209.             $this->_stringTable = array();
  210.             for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  211.                 $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
  212.             }
  213.  
  214.             // Create styles dictionaries
  215.             $this->_stylesConditionalHashTable->addFromSource(     $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet)             );
  216.             $this->_fillHashTable->addFromSource(                 $this->getWriterPart('Style')->allFills($this->_spreadSheet)             );
  217.             $this->_fontHashTable->addFromSource(                 $this->getWriterPart('Style')->allFonts($this->_spreadSheet)             );
  218.             $this->_bordersHashTable->addFromSource(             $this->getWriterPart('Style')->allBorders($this->_spreadSheet)             );
  219.             $this->_numFmtHashTable->addFromSource(             $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet)     );
  220.  
  221.             // Create drawing dictionary
  222.             $this->_drawingHashTable->addFromSource(             $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet)         );
  223.  
  224.             // Create new ZIP file and open it for writing
  225.             $objZip = new ZipArchive();
  226.  
  227.             // Try opening the ZIP file
  228.             if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
  229.                 if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
  230.                     throw new Exception("Could not open " . $pFilename . " for writing.");
  231.                 }
  232.             }
  233.  
  234.             // Add [Content_Types].xml to ZIP file
  235.             $objZip->addFromString('[Content_Types].xml',             $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
  236.  
  237.             // Add relationships to ZIP file
  238.             $objZip->addFromString('_rels/.rels',                     $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  239.             $objZip->addFromString('xl/_rels/workbook.xml.rels',     $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  240.  
  241.             // Add document properties to ZIP file
  242.             $objZip->addFromString('docProps/app.xml',                 $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  243.             $objZip->addFromString('docProps/core.xml',             $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  244.  
  245.             // Add theme to ZIP file
  246.             $objZip->addFromString('xl/theme/theme1.xml',             $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  247.  
  248.             // Add string table to ZIP file
  249.             $objZip->addFromString('xl/sharedStrings.xml',             $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  250.  
  251.             // Add styles to ZIP file
  252.             $objZip->addFromString('xl/styles.xml',                 $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  253.  
  254.             // Add workbook to ZIP file
  255.             $objZip->addFromString('xl/workbook.xml',                 $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
  256.  
  257.             // Add worksheets
  258.             for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  259.                 $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable));
  260.             }
  261.  
  262.             // Add worksheet relationships (drawings, ...)
  263.             for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  264.  
  265.                 // Add relationships
  266.                 $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels',     $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
  267.  
  268.                 // Add drawing relationship parts
  269.                 if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
  270.                     // Drawing relationships
  271.                     $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
  272.  
  273.                     // Drawings
  274.                     $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
  275.                 }
  276.  
  277.                 // Add comment relationship parts
  278.                 if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
  279.                     // VML Comments
  280.                     $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
  281.  
  282.                     // Comments
  283.                     $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
  284.                 }
  285.  
  286.                 // Add header/footer relationship parts
  287.                 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
  288.                     // VML Drawings
  289.                     $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
  290.  
  291.                     // VML Drawing relationships
  292.                     $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
  293.  
  294.                     // Media
  295.                     foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
  296.                         $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
  297.                     }
  298.                 }
  299.             }
  300.  
  301.             // Add media
  302.             for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
  303.                 if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  304.                     $imageContents = null;
  305.                     $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
  306.  
  307.                     if (strpos($imagePath, 'zip://') !== false) {
  308.                         $imagePath = substr($imagePath, 6);
  309.                         $imagePathSplitted = explode('#', $imagePath);
  310.  
  311.                         $imageZip = new ZipArchive();
  312.                         $imageZip->open($imagePathSplitted[0]);
  313.                         $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
  314.                         $imageZip->close();
  315.                         unset($imageZip);
  316.                     } else {
  317.                         $imageContents = file_get_contents($imagePath);
  318.                     }
  319.  
  320.                     $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  321.                 } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  322.                     ob_start();
  323.                     call_user_func(
  324.                         $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  325.                         $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  326.                     );
  327.                     $imageContents = ob_get_contents();
  328.                     ob_end_clean();
  329.  
  330.                     $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  331.                 }
  332.             }
  333.  
  334.             PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  335.             PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
  336.  
  337.             // Close file
  338.             if ($objZip->close() === false) {
  339.                 throw new Exception("Could not close zip file $pFilename.");
  340.             }
  341.  
  342.             // If a temporary file was used, copy it to the correct file stream
  343.             if ($originalFilename != $pFilename) {
  344.                 if (copy($pFilename, $originalFilename) === false) {
  345.                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  346.                 }
  347.                 @unlink($pFilename);
  348.             }
  349.         } else {
  350.             throw new Exception("PHPExcel object unassigned.");
  351.         }
  352.     }
  353.  
  354.     /**
  355.      * Get PHPExcel object
  356.      *
  357.      * @return PHPExcel 
  358.      * @throws Exception
  359.      */
  360.     public function getPHPExcel() {
  361.         if (!is_null($this->_spreadSheet)) {
  362.             return $this->_spreadSheet;
  363.         } else {
  364.             throw new Exception("No PHPExcel assigned.");
  365.         }
  366.     }
  367.  
  368.     /**
  369.      * Set PHPExcel object
  370.      *
  371.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  372.      * @throws    Exception
  373.      * @return PHPExcel_Writer_Excel2007 
  374.      */
  375.     public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  376.         $this->_spreadSheet = $pPHPExcel;
  377.         return $this;
  378.     }
  379.  
  380.     /**
  381.      * Get string table
  382.      *
  383.      * @return string[] 
  384.      */
  385.     public function getStringTable() {
  386.         return $this->_stringTable;
  387.     }
  388.  
  389.     /**
  390.      * Get PHPExcel_Style_Conditional HashTable
  391.      *
  392.      * @return PHPExcel_HashTable 
  393.      */
  394.     public function getStylesConditionalHashTable() {
  395.         return $this->_stylesConditionalHashTable;
  396.     }
  397.  
  398.     /**
  399.      * Get PHPExcel_Style_Fill HashTable
  400.      *
  401.      * @return PHPExcel_HashTable 
  402.      */
  403.     public function getFillHashTable() {
  404.         return $this->_fillHashTable;
  405.     }
  406.  
  407.     /**
  408.      * Get PHPExcel_Style_Font HashTable
  409.      *
  410.      * @return PHPExcel_HashTable 
  411.      */
  412.     public function getFontHashTable() {
  413.         return $this->_fontHashTable;
  414.     }
  415.  
  416.     /**
  417.      * Get PHPExcel_Style_Borders HashTable
  418.      *
  419.      * @return PHPExcel_HashTable 
  420.      */
  421.     public function getBordersHashTable() {
  422.         return $this->_bordersHashTable;
  423.     }
  424.  
  425.     /**
  426.      * Get PHPExcel_Style_NumberFormat HashTable
  427.      *
  428.      * @return PHPExcel_HashTable 
  429.      */
  430.     public function getNumFmtHashTable() {
  431.         return $this->_numFmtHashTable;
  432.     }
  433.  
  434.     /**
  435.      * Get PHPExcel_Worksheet_BaseDrawing HashTable
  436.      *
  437.      * @return PHPExcel_HashTable 
  438.      */
  439.     public function getDrawingHashTable() {
  440.         return $this->_drawingHashTable;
  441.     }
  442.  
  443.     /**
  444.      * Get Pre-Calculate Formulas
  445.      *
  446.      * @return boolean 
  447.      */
  448.     public function getPreCalculateFormulas() {
  449.         return $this->_preCalculateFormulas;
  450.     }
  451.  
  452.     /**
  453.      * Set Pre-Calculate Formulas
  454.      *
  455.      * @param boolean $pValue    Pre-Calculate Formulas?
  456.      */
  457.     public function setPreCalculateFormulas($pValue = true) {
  458.         $this->_preCalculateFormulas = $pValue;
  459.     }
  460.  
  461.     /**
  462.      * Get Office2003 compatibility
  463.      *
  464.      * @return boolean 
  465.      */
  466.     public function getOffice2003Compatibility() {
  467.         return $this->_office2003compatibility;
  468.     }
  469.  
  470.     /**
  471.      * Set Pre-Calculate Formulas
  472.      *
  473.      * @param boolean $pValue    Office2003 compatibility?
  474.      * @return PHPExcel_Writer_Excel2007 
  475.      */
  476.     public function setOffice2003Compatibility($pValue = false) {
  477.         $this->_office2003compatibility = $pValue;
  478.         return $this;
  479.     }
  480.  
  481.     /**
  482.      * Get use disk caching where possible?
  483.      *
  484.      * @return boolean 
  485.      */
  486.     public function getUseDiskCaching() {
  487.         return $this->_useDiskCaching;
  488.     }
  489.  
  490.     /**
  491.      * Set use disk caching where possible?
  492.      *
  493.      * @param     boolean     $pValue 
  494.      * @param    string        $pDirectory        Disk caching directory
  495.      * @throws    Exception    Exception when directory does not exist
  496.      * @return PHPExcel_Writer_Excel2007 
  497.      */
  498.     public function setUseDiskCaching($pValue = false, $pDirectory = null) {
  499.         $this->_useDiskCaching = $pValue;
  500.  
  501.         if (!is_null($pDirectory)) {
  502.             if (is_dir($pDirectory)) {
  503.                 $this->_diskCachingDirectory = $pDirectory;
  504.             } else {
  505.                 throw new Exception("Directory does not exist: $pDirectory");
  506.             }
  507.         }
  508.         return $this;
  509.     }
  510.  
  511.     /**
  512.      * Get disk caching directory
  513.      *
  514.      * @return string 
  515.      */
  516.     public function getDiskCachingDirectory() {
  517.         return $this->_diskCachingDirectory;
  518.     }
  519. }

Documentation generated on Thu, 26 Aug 2010 17:42:08 +0200 by phpDocumentor 1.4.3