CreateExcelSharedStrings.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Generate excel shared strings
  4. *
  5. * @category Phpdocx
  6. * @package elements
  7. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  8. * (http://www.2mdc.com)
  9. * @license LGPL
  10. * @version 1.0
  11. * @link http://www.phpdocx.com
  12. * @since File available since Release 1.0
  13. */
  14. include_once dirname(__FILE__) . '/CreateElement.inc';
  15. /**
  16. * Generate excel shared strings
  17. *
  18. * @category Phpdocx
  19. * @package elements
  20. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  21. * (http://www.2mdc.com)
  22. * @license http://www.phpdocx.com/wp-content/themes/lightword/pro_license.php
  23. * @version 1.0
  24. * @link http://www.phpdocx.com
  25. * @since Class available since Release 1.0
  26. */
  27. class CreateExcelSharedStrings extends CreateElement
  28. {
  29. /**
  30. *
  31. * @var CreateExcelSharedStrings
  32. * @access private
  33. * @static
  34. */
  35. private static $_instance = NULL;
  36. /**
  37. * Construct
  38. *
  39. * @access public
  40. */
  41. public function __construct()
  42. {
  43. }
  44. /**
  45. * Destruct
  46. *
  47. * @access public
  48. */
  49. public function __destruct()
  50. {
  51. }
  52. /**
  53. *
  54. * @return string
  55. * @access public
  56. */
  57. public function __toString()
  58. {
  59. return $this->_xml;
  60. }
  61. /**
  62. *
  63. * @return CreateExcelSharedStrings
  64. * @static
  65. */
  66. public static function getInstance()
  67. {
  68. if (self::$_instance == NULL) {
  69. self::$_instance = new CreateExcelSharedStrings();
  70. }
  71. return self::$_instance;
  72. }
  73. /**
  74. * Create excel shared strings
  75. *
  76. * @param string $args[0]
  77. * @param string $args[1]
  78. * @access public
  79. */
  80. public function createExcelSharedStrings()
  81. {
  82. $this->_xml = '';
  83. $args = func_get_args();
  84. $type = $args[1];
  85. $dats = $args[0];
  86. $tamDatos = count($dats);
  87. foreach ($dats as $ind => $data) {
  88. $tamCols = count($data);
  89. break;
  90. }
  91. $tamDatos = count($dats);
  92. if (strpos($type, 'pie') !== false) {
  93. $tamCols = 1;
  94. } else {
  95. $tamDatos--;
  96. }
  97. $this->generateSST($tamDatos + $tamCols + 2);
  98. for ($i = 0; $i < $tamCols; $i++) {
  99. if (strpos($type, 'pie') !== false) {
  100. $this->generateSI();
  101. $this->generateT('0');
  102. break;
  103. }else
  104. $this->generateSI();
  105. $this->generateT($dats[0][$i]);
  106. }
  107. foreach ($dats as $ind => $val) {
  108. if ($ind == '0')
  109. continue;
  110. $this->generateSI();
  111. $this->generateT($ind);
  112. }
  113. $this->generateSI();
  114. $this->generateT(' ', 'preserve');
  115. $msg = 'To change the range size of values,
  116. drag the bottom right corner';
  117. $this->generateSI();
  118. $this->generateT($msg);
  119. $this->cleanTemplate();
  120. }
  121. /**
  122. * Generate sst
  123. *
  124. * @param string $num
  125. * @access protected
  126. */
  127. protected function generateSST($num)
  128. {
  129. $this->_xml = '<?xml version="1.0" encoding="UTF-8" '.
  130. 'standalone="yes" ?><sst xmlns="http://schemas.'.
  131. 'openxmlformats.org/spreadsheetml/2006/main" '.
  132. 'count="' . $num . '" uniqueCount="' . $num .
  133. '">__GENERATESST__</sst>';
  134. }
  135. /**
  136. * Generate si
  137. * @access protected
  138. */
  139. protected function generateSI()
  140. {
  141. $xml = '<si>__GENERATESI__</si>__GENERATESST__';
  142. $this->_xml = str_replace('__GENERATESST__', $xml, $this->_xml);
  143. }
  144. /**
  145. * Generate t
  146. *
  147. * @param string $name
  148. * @param string $space
  149. * @access protected
  150. */
  151. protected function generateT($name, $space = '')
  152. {
  153. $xmlAux = '<t';
  154. if ($space != '')
  155. $xmlAux .= ' xml:space="' . $space . '"';
  156. $xmlAux .= '>' . $name . '</t>';
  157. $this->_xml = str_replace('__GENERATESI__', $xmlAux, $this->_xml);
  158. }
  159. }