StringTable.php 13 KB

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