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

Source for file Excel2007.php

Documentation is available at Excel2007.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.4, 2008-10-27
  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
  89.  * @copyright  Copyright (c) 2006 - 2008 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_Drawing 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.                 }
  373.                 //The line underneath does not support adding a file from a ZIP archive, the line above does!
  374.                 //$objZip->addFile($this->getDrawingHashTable()->getByIndex($i)->getPath(), 'xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getFilename());
  375.             }
  376.  
  377.             PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  378.  
  379.             // Close file
  380.             if ($objZip->close(=== false{
  381.                 throw new Exception("Could not close zip file $pFilename.");
  382.             }
  383.  
  384.             // If a temporary file was used, copy it to the correct file stream
  385.             if ($originalFilename != $pFilename{
  386.                 if (copy($pFilename$originalFilename=== false{
  387.                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  388.                 }
  389.                 @unlink($pFilename);
  390.             }
  391.         else {
  392.             throw new Exception("PHPExcel object unassigned.");
  393.         }
  394.     }
  395.  
  396.     /**
  397.      * Get PHPExcel object
  398.      *
  399.      * @return PHPExcel 
  400.      * @throws Exception
  401.      */
  402.     public function getPHPExcel({
  403.         if (!is_null($this->_spreadSheet)) {
  404.             return $this->_spreadSheet;
  405.         else {
  406.             throw new Exception("No PHPExcel assigned.");
  407.         }
  408.     }
  409.  
  410.     /**
  411.      * Get PHPExcel object
  412.      *
  413.      * @param     PHPExcel     $pPHPExcel    PHPExcel object
  414.      * @throws    Exception
  415.      */
  416.     public function setPHPExcel(PHPExcel $pPHPExcel null{
  417.         $this->_spreadSheet = $pPHPExcel;
  418.     }
  419.  
  420.     /**
  421.      * Get string table
  422.      *
  423.      * @return string[] 
  424.      */
  425.     public function getStringTable({
  426.         return $this->_stringTable;
  427.     }
  428.  
  429.     /**
  430.      * Get PHPExcel_Style HashTable
  431.      *
  432.      * @return PHPExcel_HashTable 
  433.      */
  434.     public function getStylesHashTable({
  435.         return $this->_stylesHashTable;
  436.     }
  437.  
  438.     /**
  439.      * Get PHPExcel_Style_Conditional HashTable
  440.      *
  441.      * @return PHPExcel_HashTable 
  442.      */
  443.     public function getStylesConditionalHashTable({
  444.         return $this->_stylesConditionalHashTable;
  445.     }
  446.  
  447.     /**
  448.      * Get PHPExcel_Style_Fill HashTable
  449.      *
  450.      * @return PHPExcel_HashTable 
  451.      */
  452.     public function getFillHashTable({
  453.         return $this->_fillHashTable;
  454.     }
  455.  
  456.     /**
  457.      * Get PHPExcel_Style_Font HashTable
  458.      *
  459.      * @return PHPExcel_HashTable 
  460.      */
  461.     public function getFontHashTable({
  462.         return $this->_fontHashTable;
  463.     }
  464.  
  465.     /**
  466.      * Get PHPExcel_Style_Borders HashTable
  467.      *
  468.      * @return PHPExcel_HashTable 
  469.      */
  470.     public function getBordersHashTable({
  471.         return $this->_bordersHashTable;
  472.     }
  473.  
  474.     /**
  475.      * Get PHPExcel_Style_NumberFormat HashTable
  476.      *
  477.      * @return PHPExcel_HashTable 
  478.      */
  479.     public function getNumFmtHashTable({
  480.         return $this->_numFmtHashTable;
  481.     }
  482.  
  483.     /**
  484.      * Get PHPExcel_Worksheet_Drawing HashTable
  485.      *
  486.      * @return PHPExcel_HashTable 
  487.      */
  488.     public function getDrawingHashTable({
  489.         return $this->_drawingHashTable;
  490.     }
  491.  
  492.     /**
  493.      * Get Pre-Calculate Formulas
  494.      *
  495.      * @return boolean 
  496.      */
  497.     public function getPreCalculateFormulas({
  498.         return $this->_preCalculateFormulas;
  499.     }
  500.  
  501.     /**
  502.      * Set Pre-Calculate Formulas
  503.      *
  504.      * @param boolean $pValue    Pre-Calculate Formulas?
  505.      */
  506.     public function setPreCalculateFormulas($pValue true{
  507.         $this->_preCalculateFormulas = $pValue;
  508.     }
  509.  
  510.     /**
  511.      * Get Office2003 compatibility
  512.      *
  513.      * @return boolean 
  514.      */
  515.     public function getOffice2003Compatibility({
  516.         return $this->_office2003compatibility;
  517.     }
  518.  
  519.     /**
  520.      * Set Pre-Calculate Formulas
  521.      *
  522.      * @param boolean $pValue    Office2003 compatibility?
  523.      */
  524.     public function setOffice2003Compatibility($pValue false{
  525.         $this->_office2003compatibility = $pValue;
  526.     }
  527.  
  528.     /**
  529.      * Get use disk caching where possible?
  530.      *
  531.      * @return boolean 
  532.      */
  533.     public function getUseDiskCaching({
  534.         return $this->_useDiskCaching;
  535.     }
  536.  
  537.     /**
  538.      * Set use disk caching where possible?
  539.      *
  540.      * @param boolean $pValue 
  541.      */
  542.     public function setUseDiskCaching($pValue false{
  543.         $this->_useDiskCaching = $pValue;
  544.     }
  545. }

Documentation generated on Mon, 27 Oct 2008 08:38:46 +0100 by phpDocumentor 1.4.1