CreateExcelTable.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Create excel table
  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. * Create excel table
  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 File available since Release 1.0
  26. */
  27. class CreateExcelTable extends CreateElement
  28. {
  29. /**
  30. *
  31. * @var CreateExcelTable
  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 CreateExcelTable
  64. * @static
  65. */
  66. public static function getInstance()
  67. {
  68. if (self::$_instance == NULL) {
  69. self::$_instance = new CreateExcelTable();
  70. }
  71. return self::$_instance;
  72. }
  73. /**
  74. * Create excel table
  75. *
  76. * @access public
  77. * @param string args[0]
  78. * @param string args[1]
  79. */
  80. public function createExcelTable()
  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->generateTABLE($tamDatos, $tamCols);
  98. $this->generateTABLECOLUMNS($tamCols + 1);
  99. $this->generateTABLECOLUMN(1, ' ');
  100. for ($i = 0; $i < $tamCols; $i++) {
  101. if (strpos($type, 'pie') !== false) {
  102. $this->generateTABLECOLUMN($i + 2, '0');
  103. break;
  104. }else
  105. $this->generateTABLECOLUMN($i + 2, $dats[0][$i]);
  106. }
  107. $this->generateTABLESTYLEINFO();
  108. $this->cleanTemplate();
  109. }
  110. /**
  111. * Generate table
  112. *
  113. * @param int $rows
  114. * @param int $cols
  115. * @access protected
  116. */
  117. protected function generateTABLE($rows, $cols)
  118. {
  119. $chart = 'A';
  120. for ($i = 0; $i < $cols; $i++)
  121. $chart++;
  122. $rows++;
  123. $this->_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'.
  124. '<table xmlns="http://schemas.openxmlformats.org/spreads'.
  125. 'heetml/2006/main" id="1" name="Tabla1" displayName='.
  126. '"Tabla1" ref="A1:' . $chart . $rows .
  127. '" totalsRowShown="0" tableBorderDxfId="0">'.
  128. '__GENERATETABLE__</table>';
  129. }
  130. /**
  131. * Generate tablecolumns
  132. *
  133. * @param string $count
  134. * @access protected
  135. */
  136. protected function generateTABLECOLUMNS($count = '2')
  137. {
  138. $xml = '<tableColumns count="' . $count .
  139. '">__GENERATETABLECOLUMNS__</tableColumns>__GENERATETABLE__';
  140. $this->_xml = str_replace('__GENERATETABLE__', $xml, $this->_xml);
  141. }
  142. /**
  143. * Generate tablecolumn
  144. *
  145. * @param string $id
  146. * @param string $name
  147. * @access protected
  148. */
  149. protected function generateTABLECOLUMN($id = '2', $name = '')
  150. {
  151. $xml = '<tableColumn id="' . $id . '" name="' . $name .
  152. '"></tableColumn >__GENERATETABLECOLUMNS__';
  153. $this->_xml = str_replace(
  154. '__GENERATETABLECOLUMNS__', $xml, $this->_xml
  155. );
  156. }
  157. /**
  158. * Generate tablestyleinfo
  159. *
  160. * @param string $showFirstColumn
  161. * @param string $showLastColumn
  162. * @param string $showRowStripes
  163. * @param string $showColumnStripes
  164. * @access protected
  165. */
  166. protected function generateTABLESTYLEINFO($showFirstColumn = '0',
  167. $showLastColumn = "0",
  168. $showRowStripes = "1",
  169. $showColumnStripes = "0")
  170. {
  171. $xml = '<tableStyleInfo showFirstColumn="' . $showFirstColumn .
  172. '" showLastColumn="' . $showLastColumn .
  173. '" showRowStripes="' . $showRowStripes .
  174. '" showColumnStripes="' . $showColumnStripes .
  175. '"></tableStyleInfo >';
  176. $this->_xml = str_replace('__GENERATETABLE__', $xml, $this->_xml);
  177. }
  178. }