PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/Writer/Excel2007/StringTable.php

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