683395e35e1a3cd211e0e114aae5923bbabc2771.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_Writer_Excel2007_StringTable
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2014 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 PHPExcel_Writer_Exception
  43. */
  44. public function createStringTable($pSheet = null, $pExistingTable = null)
  45. {
  46. if ($pSheet !== NULL) {
  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 (($pExistingTable !== NULL) && 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. ($cellValue !== NULL) &&
  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] = true;
  68. } elseif ($cellValue instanceof PHPExcel_RichText &&
  69. ($cellValue !== NULL) &&
  70. !isset($aFlippedStringTable[$cellValue->getHashCode()])) {
  71. $aStringTable[] = $cellValue;
  72. $aFlippedStringTable[$cellValue->getHashCode()] = true;
  73. }
  74. }
  75. // Return
  76. return $aStringTable;
  77. } else {
  78. throw new PHPExcel_Writer_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 PHPExcel_Writer_Exception
  87. */
  88. public function writeStringTable($pStringTable = null)
  89. {
  90. if ($pStringTable !== NULL) {
  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 PHPExcel_Writer_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. * @param string $prefix Optional Namespace prefix
  133. * @throws PHPExcel_Writer_Exception
  134. */
  135. public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null, $prefix=NULL)
  136. {
  137. if ($prefix !== NULL)
  138. $prefix .= ':';
  139. // Loop through rich text elements
  140. $elements = $pRichText->getRichTextElements();
  141. foreach ($elements as $element) {
  142. // r
  143. $objWriter->startElement($prefix.'r');
  144. // rPr
  145. if ($element instanceof PHPExcel_RichText_Run) {
  146. // rPr
  147. $objWriter->startElement($prefix.'rPr');
  148. // rFont
  149. $objWriter->startElement($prefix.'rFont');
  150. $objWriter->writeAttribute('val', $element->getFont()->getName());
  151. $objWriter->endElement();
  152. // Bold
  153. $objWriter->startElement($prefix.'b');
  154. $objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
  155. $objWriter->endElement();
  156. // Italic
  157. $objWriter->startElement($prefix.'i');
  158. $objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
  159. $objWriter->endElement();
  160. // Superscript / subscript
  161. if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
  162. $objWriter->startElement($prefix.'vertAlign');
  163. if ($element->getFont()->getSuperScript()) {
  164. $objWriter->writeAttribute('val', 'superscript');
  165. } else if ($element->getFont()->getSubScript()) {
  166. $objWriter->writeAttribute('val', 'subscript');
  167. }
  168. $objWriter->endElement();
  169. }
  170. // Strikethrough
  171. $objWriter->startElement($prefix.'strike');
  172. $objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
  173. $objWriter->endElement();
  174. // Color
  175. $objWriter->startElement($prefix.'color');
  176. $objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
  177. $objWriter->endElement();
  178. // Size
  179. $objWriter->startElement($prefix.'sz');
  180. $objWriter->writeAttribute('val', $element->getFont()->getSize());
  181. $objWriter->endElement();
  182. // Underline
  183. $objWriter->startElement($prefix.'u');
  184. $objWriter->writeAttribute('val', $element->getFont()->getUnderline());
  185. $objWriter->endElement();
  186. $objWriter->endElement();
  187. }
  188. // t
  189. $objWriter->startElement($prefix.'t');
  190. $objWriter->writeAttribute('xml:space', 'preserve');
  191. $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() ));
  192. $objWriter->endElement();
  193. $objWriter->endElement();
  194. }
  195. }
  196. /**
  197. * Write Rich Text
  198. *
  199. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  200. * @param string|PHPExcel_RichText $pRichText text string or Rich text
  201. * @param string $prefix Optional Namespace prefix
  202. * @throws PHPExcel_Writer_Exception
  203. */
  204. public function writeRichTextForCharts(PHPExcel_Shared_XMLWriter $objWriter = null, $pRichText = null, $prefix=NULL)
  205. {
  206. if (!$pRichText instanceof PHPExcel_RichText) {
  207. $textRun = $pRichText;
  208. $pRichText = new PHPExcel_RichText();
  209. $pRichText->createTextRun($textRun);
  210. }
  211. if ($prefix !== NULL)
  212. $prefix .= ':';
  213. // Loop through rich text elements
  214. $elements = $pRichText->getRichTextElements();
  215. foreach ($elements as $element) {
  216. // r
  217. $objWriter->startElement($prefix.'r');
  218. // rPr
  219. $objWriter->startElement($prefix.'rPr');
  220. // Bold
  221. $objWriter->writeAttribute('b', ($element->getFont()->getBold() ? 1 : 0));
  222. // Italic
  223. $objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? 1 : 0));
  224. // Underline
  225. $underlineType = $element->getFont()->getUnderline();
  226. switch($underlineType) {
  227. case 'single' :
  228. $underlineType = 'sng';
  229. break;
  230. case 'double' :
  231. $underlineType = 'dbl';
  232. break;
  233. }
  234. $objWriter->writeAttribute('u', $underlineType);
  235. // Strikethrough
  236. $objWriter->writeAttribute('strike', ($element->getFont()->getStrikethrough() ? 'sngStrike' : 'noStrike'));
  237. // rFont
  238. $objWriter->startElement($prefix.'latin');
  239. $objWriter->writeAttribute('typeface', $element->getFont()->getName());
  240. $objWriter->endElement();
  241. // Superscript / subscript
  242. // if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
  243. // $objWriter->startElement($prefix.'vertAlign');
  244. // if ($element->getFont()->getSuperScript()) {
  245. // $objWriter->writeAttribute('val', 'superscript');
  246. // } else if ($element->getFont()->getSubScript()) {
  247. // $objWriter->writeAttribute('val', 'subscript');
  248. // }
  249. // $objWriter->endElement();
  250. // }
  251. //
  252. $objWriter->endElement();
  253. // t
  254. $objWriter->startElement($prefix.'t');
  255. // $objWriter->writeAttribute('xml:space', 'preserve'); // Excel2010 accepts, Excel2007 complains
  256. $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() ));
  257. $objWriter->endElement();
  258. $objWriter->endElement();
  259. }
  260. }
  261. /**
  262. * Flip string table (for index searching)
  263. *
  264. * @param array $stringTable Stringtable
  265. * @return array
  266. */
  267. public function flipStringTable($stringTable = array()) {
  268. // Return value
  269. $returnValue = array();
  270. // Loop through stringtable and add flipped items to $returnValue
  271. foreach ($stringTable as $key => $value) {
  272. if (! $value instanceof PHPExcel_RichText) {
  273. $returnValue[$value] = $key;
  274. } else if ($value instanceof PHPExcel_RichText) {
  275. $returnValue[$value->getHashCode()] = $key;
  276. }
  277. }
  278. // Return
  279. return $returnValue;
  280. }
  281. }