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

Documentation generated on Sun, 27 Feb 2011 16:34:16 -0800 by phpDocumentor 1.4.3