CreateExcelSheet.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * Create excel sheet
  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 sheet
  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 CreateExcelSheet extends CreateElement
  28. {
  29. /**
  30. *
  31. * @var CreateExcelSheet
  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 CreateExcelSheet
  64. * @access public
  65. * @static
  66. */
  67. public static function getInstance()
  68. {
  69. if (self::$_instance == NULL) {
  70. self::$_instance = new CreateExcelSheet();
  71. }
  72. return self::$_instance;
  73. }
  74. /**
  75. * Create excel sheet
  76. *
  77. * @access public
  78. * @param string $args[0]
  79. * @param array $args[1]
  80. */
  81. public function createExcelSheet()
  82. {
  83. $this->_xml = '';
  84. $args = func_get_args();
  85. $type = $args[1];
  86. $dats = $args[0];
  87. $sizeDats = count($dats);
  88. foreach ($dats as $ind => $data) {
  89. $sizeCols = count($data);
  90. break;
  91. }
  92. $sizeDats = count($dats);
  93. if (strpos($type, 'pie') !== false) {
  94. $sizeCols = 1;
  95. } else {
  96. $sizeDats--;
  97. }
  98. $this->generateWORKSHEET();
  99. $this->generateDIMENSION($sizeDats, $sizeCols);
  100. $this->generateSHEETVIEWS();
  101. $this->generateSHEETVIEW();
  102. $this->generateSELECTION($sizeDats + $sizeCols);
  103. $this->generateSHEETFORMATPR();
  104. $this->generateCOLS();
  105. $this->generateCOL();
  106. $this->generateSHEETDATA();
  107. $row = 1;
  108. foreach ($dats as $ind => $val) {
  109. $col = 1;
  110. $letra = 'A';
  111. $this->generateROW($row, $sizeCols);
  112. $this->generateC($letra . $row, '', 's');
  113. $this->generateV($sizeDats + $sizeCols);
  114. $letra++;
  115. foreach ($val as $valores) {
  116. $this->generateC($letra . $row, '', 's');
  117. $this->generateV($col - 1);
  118. $col++;
  119. $letra++;
  120. }
  121. $this->cleanTemplateROW();
  122. $row++;
  123. break;
  124. }
  125. foreach ($dats as $ind => $val) {
  126. if ($ind == '0')
  127. continue;
  128. $this->generateROW($row, $sizeCols);
  129. $col = 1;
  130. $letra = 'A';
  131. $this->generateC($letra . $row, 1, 's');
  132. $this->generateV($sizeCols + $row - 2);
  133. $letra++;
  134. foreach ($val as $valores) {
  135. $s = '';
  136. if ($col != $sizeCols)
  137. $s = 1;
  138. $this->generateC($letra . $row, $s);
  139. $this->generateV($valores);
  140. $col++;
  141. $letra++;
  142. }
  143. $row++;
  144. $this->cleanTemplateROW();
  145. }
  146. $this->generateROW($row + 1, $sizeCols);
  147. $row++;
  148. $this->generateC('B' . $row, 2, 's');
  149. $this->generateV($sizeDats + $sizeCols + 1);
  150. $this->generatePAGEMARGINS();
  151. $this->generateTABLEPARTS();
  152. $this->generateTABLEPART(1);
  153. $this->cleanTemplate();
  154. }
  155. /**
  156. * Generate worksheet
  157. *
  158. * @access protected
  159. */
  160. protected function generateWORKSHEET()
  161. {
  162. $this->_xml = '<?xml version="1.0" encoding="UTF-8" ' .
  163. 'standalone="yes" ?><worksheet ' .
  164. 'xmlns="http://schemas.openxmlformats.org/' .
  165. 'spreadsheetml/2006/main" ' . 'xmlns:r="http://schemas.' .
  166. 'openxmlformats.org/officeDocument/2006/relationships"' .
  167. '>__GENERATEWORKSHEET__</worksheet>';
  168. }
  169. /**
  170. * Generate dimension
  171. *
  172. * @param int $sizeX
  173. * @param int $sizeY
  174. * @access protected
  175. */
  176. protected function generateDIMENSION($sizeX, $sizeY)
  177. {
  178. $char = 'A';
  179. for ($i = 0; $i < $sizeY; $i++)
  180. $char++;
  181. $sizeX += $sizeY;
  182. $xml = '<dimension ref="A1:' . $char . $sizeX .
  183. '"></dimension>__GENERATEWORKSHEET__';
  184. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  185. }
  186. /**
  187. * Generate sheetviews
  188. *
  189. * @access protected
  190. */
  191. protected function generateSHEETVIEWS()
  192. {
  193. $xml = '<sheetViews>__GENERATESHEETVIEWS__' .
  194. '</sheetViews>__GENERATEWORKSHEET__';
  195. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  196. }
  197. /**
  198. * Generate sheetview
  199. *
  200. * @param string $tabSelected
  201. * @param string $workbookViewId
  202. * @access protected
  203. */
  204. protected function generateSHEETVIEW($tabSelected = '1',
  205. $workbookViewId = '0')
  206. {
  207. $xml = '<sheetView tabSelected="' . $tabSelected .
  208. '" workbookViewId="' . $workbookViewId .
  209. '">__GENERATESHEETVIEW__</sheetView>';
  210. $this->_xml = str_replace('__GENERATESHEETVIEWS__', $xml, $this->_xml);
  211. }
  212. /**
  213. * Generate selection
  214. *
  215. * @param string $num
  216. * @access protected
  217. */
  218. protected function generateSELECTION($num)
  219. {
  220. $xml = '<selection activeCell="B' . $num .
  221. '" sqref="B' . $num . '"></selection>';
  222. $this->_xml = str_replace('__GENERATESHEETVIEW__', $xml, $this->_xml);
  223. }
  224. /**
  225. * Generate sheetformatpr
  226. *
  227. * @param string $baseColWidth
  228. * @param string $defaultRowHeight
  229. * @access protected
  230. */
  231. protected function generateSHEETFORMATPR($baseColWidth = '10',
  232. $defaultRowHeight = '15')
  233. {
  234. $xml = '<sheetFormatPr baseColWidth="' . $baseColWidth .
  235. '" defaultRowHeight="' . $defaultRowHeight .
  236. '"></sheetFormatPr>__GENERATEWORKSHEET__';
  237. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  238. }
  239. /**
  240. * Generate cols
  241. *
  242. * @access protected
  243. */
  244. protected function generateCOLS()
  245. {
  246. $xml = '<cols>__GENERATECOLS__</cols>__GENERATEWORKSHEET__';
  247. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  248. }
  249. /**
  250. * Generate col
  251. *
  252. * @param string $min
  253. * @param string $max
  254. * @param string $width
  255. * @param string $customWidth
  256. * @access protected
  257. */
  258. protected function generateCOL($min = '1', $max = '1',
  259. $width = '11.85546875', $customWidth = '1')
  260. {
  261. $xml = '<col min="' . $min . '" max="' . $max . '" width="' . $width .
  262. '" customWidth="' . $customWidth . '"></col>';
  263. $this->_xml = str_replace('__GENERATECOLS__', $xml, $this->_xml);
  264. }
  265. /**
  266. * Generate sheetdata
  267. *
  268. * @access protected
  269. */
  270. protected function generateSHEETDATA()
  271. {
  272. $xml = '<sheetData>__GENERATESHEETDATA__</sheetData>' .
  273. '__GENERATEWORKSHEET__';
  274. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  275. }
  276. /**
  277. * Generate row
  278. *
  279. * @param string $r
  280. * @param string $spans
  281. * @access protected
  282. */
  283. protected function generateROW($r, $spans)
  284. {
  285. $spans = '1:' . ($spans + 1);
  286. $xml = '<row r="' . $r . '" spans="' . $spans .
  287. '">__GENERATEROW__</row>__GENERATESHEETDATA__';
  288. $this->_xml = str_replace('__GENERATESHEETDATA__', $xml, $this->_xml);
  289. }
  290. /**
  291. * Generate c
  292. *
  293. * @param string $r
  294. * @param string $s
  295. * @param string $t
  296. * @access protected
  297. */
  298. protected function generateC($r, $s, $t = '')
  299. {
  300. $xmlAux = '<c r="' . $r . '"';
  301. if ($s != '') {
  302. $xmlAux .= ' s="' . $s . '"';
  303. }
  304. if ($t != '') {
  305. $xmlAux .= ' t="' . $t . '"';
  306. }
  307. $xmlAux .= '>__GENERATEC__</c>__GENERATEROW__';
  308. $this->_xml = str_replace('__GENERATEROW__', $xmlAux, $this->_xml);
  309. }
  310. /**
  311. * Generate v
  312. *
  313. * @param string $num
  314. * @access protected
  315. */
  316. protected function generateV($num)
  317. {
  318. $this->_xml = str_replace(
  319. '__GENERATEC__', '<v>' . $num . '</v>', $this->_xml
  320. );
  321. }
  322. /**
  323. * Generate pagemargins
  324. *
  325. * @param string $left
  326. * @param string $rigth
  327. * @param string $bottom
  328. * @param string $top
  329. * @param string $header
  330. * @param string $footer
  331. * @access protected
  332. */
  333. protected function generatePAGEMARGINS($left = '0.7', $rigth = '0.7',
  334. $bottom = '0.75', $top = '0.75',
  335. $header = '0.3', $footer = '0.3')
  336. {
  337. $xml = '<pageMargins left="' . $left . '" right="' . $rigth .
  338. '" top="' . $top . '" bottom="' . $bottom .
  339. '" header="' . $header . '" footer="' . $footer .
  340. '"></pageMargins>__GENERATEWORKSHEET__';
  341. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  342. }
  343. /**
  344. * Generate pagesetup
  345. *
  346. * @param string $paperSize
  347. * @param string $orientation
  348. * @param string $id
  349. * @access protected
  350. */
  351. protected function generatePAGESETUP($paperSize = '9',
  352. $orientation = 'portrait', $id = '1')
  353. {
  354. $xml = '<pageSetup paperSize="' . $paperSize .
  355. '" orientation="' . $orientation . '" r:id="rId' . $id .
  356. '"></pageSetup>__GENERATEWORKSHEET__';
  357. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  358. }
  359. /**
  360. * Generate tableparts
  361. *
  362. * @param string $count
  363. * @access protected
  364. */
  365. protected function generateTABLEPARTS($count = '1')
  366. {
  367. $xml = '<tableParts count="' . $count .
  368. '">__GENERATETABLEPARTS__</tableParts>';
  369. $this->_xml = str_replace('__GENERATEWORKSHEET__', $xml, $this->_xml);
  370. }
  371. /**
  372. * Generate tablepart
  373. *
  374. * @param string $id
  375. * @access protected
  376. */
  377. protected function generateTABLEPART($id = '1')
  378. {
  379. $xml = '<tablePart r:id="rId' . $id . '"></tablePart>';
  380. $this->_xml = str_replace('__GENERATETABLEPARTS__', $xml, $this->_xml);
  381. }
  382. /**
  383. * Clean template row tags
  384. *
  385. * @access private
  386. */
  387. private function cleanTemplateROW()
  388. {
  389. $this->_xml = str_replace('__GENERATEROW__', '', $this->_xml);
  390. }
  391. }