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

Documentation generated on Mon, 05 Jan 2009 20:37:07 +0100 by phpDocumentor 1.4.1