CreateXlsx.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Create XLSX
  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. /**
  15. * Create XLSX
  16. *
  17. * @category Phpdocx
  18. * @package elements
  19. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  20. * (http://www.2mdc.com)
  21. * @license http://www.phpdocx.com/wp-content/themes/lightword/pro_license.php
  22. * @version 1.0
  23. * @link http://www.phpdocx.com
  24. * @since Class available since Release 1.0
  25. */
  26. class CreateXlsx
  27. {
  28. /**
  29. * @access private
  30. * @var CreateFooter
  31. * @static
  32. */
  33. private static $_instance = NULL;
  34. /**
  35. *
  36. * @access private
  37. * @var <type>
  38. */
  39. private $_zipXlsx;
  40. /**
  41. *
  42. * @access private
  43. * @var <type>
  44. */
  45. private $_xmlXlTablesContent;
  46. /**
  47. *
  48. * @access private
  49. * @var <type>
  50. */
  51. private $_xmlXlSharedStringsContent;
  52. /**
  53. *
  54. * @access private
  55. * @var <type>
  56. */
  57. private $_xmlXlSheetContent;
  58. /**
  59. * Construct
  60. *
  61. * @access public
  62. */
  63. public function __construct()
  64. {
  65. }
  66. /**
  67. * Destruct
  68. *
  69. * @access public
  70. */
  71. public function __destruct()
  72. {
  73. }
  74. /**
  75. *
  76. * @access public
  77. * @return string
  78. */
  79. public function __toString()
  80. {
  81. return $this->_zipXlsx;
  82. }
  83. /**
  84. *
  85. * @access public
  86. * @return CreateXlsx
  87. * @static
  88. */
  89. public static function getInstance()
  90. {
  91. if (self::$_instance == NULL) {
  92. self::$_instance = new CreateXlsx();
  93. }
  94. return self::$_instance;
  95. }
  96. /**
  97. * Create XLSX
  98. *
  99. * @access public
  100. * @return bool
  101. */
  102. public function createXlsx()
  103. {
  104. $args = func_get_args();
  105. $this->_zipXlsx = new ZipArchive();
  106. $this->_zipXlsx->open($args[0], ZipArchive::OVERWRITE);
  107. $this->_xmlXlTablesContent = '';
  108. $this->_xmlXlSharedStringsContent = '';
  109. $this->_xmlXlSheetContent = '';
  110. try {
  111. $dirname = substr(__FILE__, 0, strpos(__FILE__, '/classes')) .
  112. '/excel/';
  113. $this->_zipXlsx->addFile(
  114. $dirname . '[Content_Types].xml',
  115. '[Content_Types].xml'
  116. );
  117. $this->_zipXlsx->addFile(
  118. $dirname . 'docProps/core.xml',
  119. 'docProps/core.xml'
  120. );
  121. $this->_zipXlsx->addFile(
  122. $dirname . 'docProps/app.xml',
  123. 'docProps/app.xml'
  124. );
  125. $this->_zipXlsx->addFile(
  126. $dirname . '_rels/.rels',
  127. '_rels/.rels'
  128. );
  129. $this->_zipXlsx->addFile(
  130. $dirname . 'xl/_rels/workbook.xml.rels',
  131. 'xl/_rels/workbook.xml.rels'
  132. );
  133. $this->_zipXlsx->addFile(
  134. $dirname . 'xl/theme/theme1.xml',
  135. 'xl/theme/theme1.xml'
  136. );
  137. $this->_zipXlsx->addFile(
  138. $dirname . 'xl/worksheets/_rels/sheet1.xml.rels',
  139. 'xl/worksheets/_rels/sheet1.xml.rels'
  140. );
  141. $this->_zipXlsx->addFile(
  142. $dirname . 'xl/styles.xml',
  143. 'xl/styles.xml'
  144. );
  145. $this->_zipXlsx->addFile(
  146. $dirname . 'xl/workbook.xml',
  147. 'xl/workbook.xml'
  148. );
  149. $this->addTable($args[1], $args[2]);
  150. $this->_zipXlsx->addFromString(
  151. 'xl/tables/table1.xml',
  152. $this->_xmlXlTablesContent
  153. );
  154. $this->addSharedStrings($args[1], $args[2]);
  155. $this->_zipXlsx->addFromString(
  156. 'xl/sharedStrings.xml',
  157. $this->_xmlXlSharedStringsContent
  158. );
  159. $this->addSheet($args[1], $args[2]);
  160. $this->_zipXlsx->addFromString(
  161. 'xl/worksheets/sheet1.xml',
  162. $this->_xmlXlSheetContent
  163. );
  164. $this->_zipXlsx->close();
  165. return true;
  166. } catch (Exception $e) {
  167. echo 'There was an error related to XLSX file: ',
  168. $e->getMessage(),
  169. "\n";
  170. return false;
  171. }
  172. }
  173. /**
  174. * Add table
  175. *
  176. * @access protected
  177. * @param array $args[0]
  178. * @param array $args[1]
  179. */
  180. protected function addTable()
  181. {
  182. $args = func_get_args();
  183. $excelTable = CreateExcelTable::getInstance();
  184. $excelTable->createExcelTable($args[0], $args[1]);
  185. $this->_xmlXlTablesContent .= (string) $excelTable;
  186. }
  187. /**
  188. * Add shared strings
  189. *
  190. * @access protected
  191. * * @param array $args[0]
  192. * @param array $args[1]
  193. */
  194. protected function addSharedStrings()
  195. {
  196. $args = func_get_args();
  197. $excelSS = CreateExcelSharedStrings::getInstance();
  198. $excelSS->createExcelSharedStrings($args[0], $args[1]);
  199. $this->_xmlXlSharedStringsContent .= (string) $excelSS;
  200. }
  201. /**
  202. * Add sheet
  203. *
  204. * @access protected
  205. * * @param array $args[0]
  206. * @param array $args[1]
  207. */
  208. protected function addSheet()
  209. {
  210. $args = func_get_args();
  211. $excelSheet = CreateExcelSheet::getInstance();
  212. $excelSheet->createExcelSheet($args[0], $args[1]);
  213. $this->_xmlXlSheetContent .= (string) $excelSheet;
  214. }
  215. /**
  216. *
  217. * @access private
  218. * @deprecated
  219. */
  220. private function cleanTemplate()
  221. {
  222. $this->_xmlWordDocumentTemplate = preg_replace(
  223. '/__[A-Z]+__/',
  224. '',
  225. $this->_xmlWordDocumentTemplate
  226. );
  227. }
  228. }