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

Documentation generated on Mon, 05 Jan 2009 20:38:35 +0100 by phpDocumentor 1.4.1