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

Documentation generated on Tue, 01 Jun 2010 17:06:11 +0200 by phpDocumentor 1.4.3