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

Source for file StringTable.php

Documentation is available at StringTable.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.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../../');
  35. }
  36.  
  37. /** PHPExcel_Writer_Excel2007 */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/Excel2007.php';
  39.  
  40. /** PHPExcel_Writer_Excel2007_WriterPart */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/Excel2007/WriterPart.php';
  42.  
  43. /** PHPExcel_Cell_DataType */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Cell/DataType.php';
  45.  
  46. /** PHPExcel_Shared_XMLWriter */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/XMLWriter.php';
  48.  
  49. /** PHPExcel_Shared_String */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/String.php';
  51.  
  52.  
  53. /**
  54.  * PHPExcel_Writer_Excel2007_StringTable
  55.  *
  56.  * @category   PHPExcel
  57.  * @package    PHPExcel_Writer_Excel2007
  58.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  59.  */
  60. {
  61.     /**
  62.      * Create worksheet stringtable
  63.      *
  64.      * @param     PHPExcel_Worksheet     $pSheet                Worksheet
  65.      * @param     string[]                 $pExistingTable     Existing table to eventually merge with
  66.      * @return     string[]                 String table for worksheet
  67.      * @throws     Exception
  68.      */
  69.     public function createStringTable($pSheet null$pExistingTable null)
  70.     {
  71.         if (!is_null($pSheet)) {
  72.             // Create string lookup table
  73.             $aStringTable array();
  74.             $cellCollection null;
  75.             $aFlippedStringTable null;    // For faster lookup
  76.         
  77.             // Is an existing table given?
  78.             if (!is_null($pExistingTable&& is_array($pExistingTable)) {
  79.                 $aStringTable $pExistingTable;
  80.             }
  81.             
  82.             // Fill index array
  83.             $aFlippedStringTable $this->flipStringTable($aStringTable);
  84.             
  85.             // Loop through cells
  86.             $cellCollection $pSheet->getCellCollection();
  87.             foreach ($cellCollection as $cell{
  88.                 if (!is_object($cell->getValue()) &&
  89.                     !isset($aFlippedStringTable[$cell->getValue()]&&
  90.                     !is_null($cell->getValue()) &&
  91.                     $cell->getValue(!== '' &&
  92.                     ($cell->getDataType(== PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType(== PHPExcel_Cell_DataType::TYPE_NULL)
  93.                 {
  94.                         $aStringTable[$cell->getValue();
  95.                         $aFlippedStringTable[$cell->getValue()1;
  96.                         
  97.                 else if ($cell->getValue(instanceof PHPExcel_RichText &&
  98.                            !isset($aFlippedStringTable[$cell->getValue()->getHashCode()]&&
  99.                            !is_null($cell->getValue())
  100.                 {
  101.                     $aStringTable[$cell->getValue();
  102.                     $aFlippedStringTable[$cell->getValue()->getHashCode()1;
  103.                 }
  104.             }
  105.  
  106.             // Return
  107.             return $aStringTable;
  108.         else {
  109.             throw new Exception("Invalid PHPExcel_Worksheet object passed.");
  110.         }
  111.     }
  112.     
  113.     /**
  114.      * Write string table to XML format
  115.      *
  116.      * @param     string[]     $pStringTable 
  117.      * @return     string         XML Output
  118.      * @throws     Exception
  119.      */
  120.     public function writeStringTable($pStringTable null)
  121.     {
  122.         if (!is_null($pStringTable)) {                    
  123.             // Create XML writer
  124.             $objWriter null;
  125.             if ($this->getParentWriter()->getUseDiskCaching()) {
  126.                 $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  127.             else {
  128.                 $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  129.             }
  130.             
  131.             // XML header
  132.             $objWriter->startDocument('1.0','UTF-8','yes');
  133.             
  134.             // String table
  135.             $objWriter->startElement('sst');
  136.             $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  137.             $objWriter->writeAttribute('uniqueCount'count($pStringTable));
  138.             
  139.                 // Loop through string table
  140.                 foreach ($pStringTable as $textElement{
  141.                     $objWriter->startElement('si');
  142.                     
  143.                         if ($textElement instanceof PHPExcel_RichText{
  144.                             $textToWrite PHPExcel_Shared_String::ControlCharacterPHP2OOXML$textElement );
  145.                             $objWriter->startElement('t');
  146.                             if ($textToWrite !== trim($textToWrite)) {
  147.                                 $objWriter->writeAttribute('xml:space''preserve');
  148.                             }
  149.                             $objWriter->writeRaw($textToWrite);
  150.                             $objWriter->endElement();
  151.                         else if ($textElement instanceof PHPExcel_RichText{
  152.                             $this->writeRichText($objWriter$textElement);
  153.                         }
  154.                         
  155.                     $objWriter->endElement();
  156.                 }
  157.                 
  158.             $objWriter->endElement();
  159.  
  160.             // Return
  161.             return $objWriter->getData();
  162.         else {
  163.             throw new Exception("Invalid string table array passed.");
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Write Rich Text
  169.      *
  170.      * @param     PHPExcel_Shared_XMLWriter        $objWriter         XML Writer
  171.      * @param     PHPExcel_RichText                $pRichText        Rich text
  172.      * @throws     Exception
  173.      */
  174.     public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter nullPHPExcel_RichText $pRichText null)
  175.     {
  176.         // Loop through rich text elements
  177.         $elements $pRichText->getRichTextElements();
  178.         foreach ($elements as $element{
  179.             // r
  180.             $objWriter->startElement('r');
  181.                 
  182.                 // rPr
  183.                 if ($element instanceof PHPExcel_RichText_Run{
  184.                     // rPr
  185.                     $objWriter->startElement('rPr');
  186.  
  187.                         // rFont
  188.                         $objWriter->startElement('rFont');
  189.                         $objWriter->writeAttribute('val'$element->getFont()->getName());
  190.                         $objWriter->endElement();
  191.                             
  192.                         // Bold
  193.                         $objWriter->startElement('b');
  194.                         $objWriter->writeAttribute('val'($element->getFont()->getBold('true' 'false'));
  195.                         $objWriter->endElement();
  196.                                     
  197.                         // Italic
  198.                         $objWriter->startElement('i');
  199.                         $objWriter->writeAttribute('val'($element->getFont()->getItalic('true' 'false'));
  200.                         $objWriter->endElement();
  201.                         
  202.                         // Superscript / subscript
  203.                         if ($element->getFont()->getSuperScript(|| $element->getFont()->getSubScript()) {
  204.                             $objWriter->startElement('vertAlign');
  205.                             if ($element->getFont()->getSuperScript()) {
  206.                                 $objWriter->writeAttribute('val''superscript');
  207.                             else if ($element->getFont()->getSubScript()) {
  208.                                 $objWriter->writeAttribute('val''subscript');
  209.                             }
  210.                             $objWriter->endElement();
  211.                         }
  212.                             
  213.                         // Strikethrough
  214.                         $objWriter->startElement('strike');
  215.                         $objWriter->writeAttribute('val'($element->getFont()->getStrikethrough('true' 'false'));
  216.                         $objWriter->endElement();            
  217.                             
  218.                         // Color
  219.                         $objWriter->startElement('color');
  220.                         $objWriter->writeAttribute('rgb'$element->getFont()->getColor()->getARGB());
  221.                         $objWriter->endElement();    
  222.                             
  223.                         // Size
  224.                         $objWriter->startElement('sz');
  225.                         $objWriter->writeAttribute('val'$element->getFont()->getSize());
  226.                         $objWriter->endElement();
  227.                             
  228.                         // Underline
  229.                         $objWriter->startElement('u');
  230.                         $objWriter->writeAttribute('val'$element->getFont()->getUnderline());
  231.                         $objWriter->endElement();
  232.                 
  233.                     $objWriter->endElement();
  234.                 }
  235.                     
  236.                 // t
  237.                 $objWriter->startElement('t');
  238.                 $objWriter->writeAttribute('xml:space''preserve');
  239.                 $objWriter->writeRaw(PHPExcel_Shared_String::ControlCharacterPHP2OOXML$element->getText() ));
  240.                 $objWriter->endElement();
  241.                     
  242.             $objWriter->endElement();
  243.         }    
  244.     }
  245.     
  246.     /**
  247.      * Flip string table (for index searching)
  248.      *
  249.      * @param     array    $stringTable    Stringtable
  250.      * @return     array 
  251.      */
  252.     public function flipStringTable($stringTable array()) {
  253.         // Return value
  254.         $returnValue array();
  255.         
  256.         // Loop through stringtable and add flipped items to $returnValue
  257.         foreach ($stringTable as $key => $value{
  258.             if ($value instanceof PHPExcel_RichText{
  259.                 $returnValue[$value$key;
  260.             else if ($value instanceof PHPExcel_RichText{
  261.                 $returnValue[$value->getHashCode()$key;
  262.             }
  263.         }
  264.  
  265.         // Return
  266.         return $returnValue;
  267.     }
  268. }

Documentation generated on Mon, 11 Jan 2010 08:14:17 +0100 by phpDocumentor 1.4.1