StringTable.php 7.7 KB

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