/branches/v1.3.0/Classes/PHPExcel/Writer/Excel2007/Worksheet.php

# · PHP · 786 lines · 434 code · 102 blank · 250 comment · 104 complexity · 0101370a826dba5a9569dc57eb5acb8b MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel, Maarten Balliauw
  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 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_Writer_Excel2007 */
  28. require_once 'PHPExcel/Writer/Excel2007.php';
  29. /** PHPExcel_Writer_Excel2007_WriterPart */
  30. require_once 'PHPExcel/Writer/Excel2007/WriterPart.php';
  31. /** PHPExcel_Cell */
  32. require_once 'PHPExcel/Cell.php';
  33. /** PHPExcel_Worksheet */
  34. require_once 'PHPExcel/Worksheet.php';
  35. /** PHPExcel_Style_Conditional */
  36. require_once 'PHPExcel/Style/Conditional.php';
  37. /** PHPExcel_Shared_Font */
  38. require_once 'PHPExcel/Shared/Font.php';
  39. /**
  40. * PHPExcel_Writer_Excel2007_Worksheet
  41. *
  42. * @category PHPExcel
  43. * @package PHPExcel_Writer_Excel2007
  44. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  45. */
  46. class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_WriterPart
  47. {
  48. /**
  49. * Write worksheet to XML format
  50. *
  51. * @param PHPExcel_Worksheet $pSheet
  52. * @param string[] $pStringTable
  53. * @return string XML Output
  54. * @throws Exception
  55. */
  56. public function writeWorksheet($pSheet = null, $pStringTable = null)
  57. {
  58. if (!is_null($pSheet)) {
  59. // Create XML writer
  60. $objWriter = new xmlWriter();
  61. $objWriter->openMemory();
  62. // XML header
  63. $objWriter->startDocument('1.0','UTF-8','yes');
  64. // Worksheet
  65. $objWriter->startElement('worksheet');
  66. $objWriter->writeAttribute('xml:space', 'preserve');
  67. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  68. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  69. // sheetPr
  70. $this->_writeSheetPr($objWriter, $pSheet);
  71. // Dimension
  72. $this->_writeDimension($objWriter, $pSheet);
  73. // sheetViews
  74. $this->_writeSheetViews($objWriter, $pSheet);
  75. // sheetFormatPr
  76. $this->_writeSheetFormatPr($objWriter);
  77. // cols
  78. $this->_writeCols($objWriter, $pSheet);
  79. // sheetData
  80. $this->_writeSheetData($objWriter, $pSheet, $pStringTable);
  81. // sheetProtection
  82. $this->_writeSheetProtection($objWriter, $pSheet);
  83. // protectedRanges
  84. $this->_writeProtectedRanges($objWriter, $pSheet);
  85. // autoFilter
  86. $this->_writeAutoFilter($objWriter, $pSheet);
  87. // mergeCells
  88. $this->_writeMergeCells($objWriter, $pSheet);
  89. // conditionalFormatting
  90. $this->_writeConditionalFormatting($objWriter, $pSheet);
  91. // Print options
  92. $this->_writePrintOptions($objWriter);
  93. // Page margins
  94. $this->_writePageMargins($objWriter, $pSheet);
  95. // Page setup
  96. $this->_writePageSetup($objWriter, $pSheet);
  97. // Header / footer
  98. $this->_writeHeaderFooter($objWriter, $pSheet);
  99. // Breaks
  100. $this->_writeBreaks($objWriter, $pSheet);
  101. // Drawings
  102. $this->_writeDrawings($objWriter, $pSheet);
  103. $objWriter->endElement();
  104. // Return
  105. return $objWriter->outputMemory(true);
  106. } else {
  107. throw new Exception("Invalid PHPExcel_Worksheet object passed.");
  108. }
  109. }
  110. /**
  111. * Write SheetPr
  112. *
  113. * @param xmlWriter $objWriter XML Writer
  114. * @param PHPExcel_Worksheet $pSheet Worksheet
  115. * @throws Exception
  116. */
  117. private function _writeSheetPr($objWriter = null, $pSheet = null)
  118. {
  119. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  120. // sheetPr
  121. $objWriter->startElement('sheetPr');
  122. $objWriter->writeAttribute('codeName', $pSheet->getTitle());
  123. $objWriter->endElement();
  124. } else {
  125. throw new Exception("Invalid parameters passed.");
  126. }
  127. }
  128. /**
  129. * Write Dimension
  130. *
  131. * @param xmlWriter $objWriter XML Writer
  132. * @param PHPExcel_Worksheet $pSheet Worksheet
  133. * @throws Exception
  134. */
  135. private function _writeDimension($objWriter = null, $pSheet = null)
  136. {
  137. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  138. // dimension
  139. $objWriter->startElement('dimension');
  140. $objWriter->writeAttribute('ref', $pSheet->calculateWorksheetDimension());
  141. $objWriter->endElement();
  142. } else {
  143. throw new Exception("Invalid parameters passed.");
  144. }
  145. }
  146. /**
  147. * Write SheetViews
  148. *
  149. * @param xmlWriter $objWriter XML Writer
  150. * @param PHPExcel_Worksheet $pSheet Worksheet
  151. * @throws Exception
  152. */
  153. private function _writeSheetViews($objWriter = null, $pSheet = null)
  154. {
  155. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  156. // sheetViews
  157. $objWriter->startElement('sheetViews');
  158. // sheetView
  159. $objWriter->startElement('sheetView');
  160. $objWriter->writeAttribute('tabSelected', '1');
  161. $objWriter->writeAttribute('workbookViewId', '0');
  162. // Pane
  163. if ($pSheet->getFreezePane() != '') {
  164. // Calculate freeze coordinates
  165. $xSplit = 0;
  166. $ySplit = 0;
  167. $topLeftCell = $pSheet->getFreezePane();
  168. list($xSplit, $ySplit) = PHPExcel_Cell::coordinateFromString($pSheet->getFreezePane());
  169. $xSplit = PHPExcel_Cell::columnIndexFromString($xSplit);
  170. // pane
  171. $objWriter->startElement('pane');
  172. $objWriter->writeAttribute('xSplit', $xSplit - 1);
  173. $objWriter->writeAttribute('ySplit', $ySplit - 1);
  174. $objWriter->writeAttribute('topLeftCell', $topLeftCell);
  175. $objWriter->writeAttribute('activePane', 'bottomRight');
  176. $objWriter->writeAttribute('state', 'frozen');
  177. $objWriter->endElement();
  178. }
  179. // Selection
  180. $objWriter->writeElement('selection', null);
  181. $objWriter->endElement();
  182. $objWriter->endElement();
  183. } else {
  184. throw new Exception("Invalid parameters passed.");
  185. }
  186. }
  187. /**
  188. * Write SheetFormatPr
  189. *
  190. * @param xmlWriter $objWriter XML Writer
  191. * @throws Exception
  192. */
  193. private function _writeSheetFormatPr($objWriter = null)
  194. {
  195. if ($objWriter instanceof xmlWriter) {
  196. // sheetFormatPr
  197. $objWriter->startElement('sheetFormatPr');
  198. $objWriter->writeAttribute('defaultRowHeight', '12.75');
  199. $objWriter->endElement();
  200. } else {
  201. throw new Exception("Invalid parameters passed.");
  202. }
  203. }
  204. /**
  205. * Write Cols
  206. *
  207. * @param xmlWriter $objWriter XML Writer
  208. * @param PHPExcel_Worksheet $pSheet Worksheet
  209. * @throws Exception
  210. */
  211. private function _writeCols($objWriter = null, $pSheet = null)
  212. {
  213. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  214. // cols
  215. $objWriter->startElement('cols');
  216. // Check if there is at least one column dimension specified. If not, create one.
  217. if (count($pSheet->getColumnDimensions()) == 0) {
  218. $pSheet->getColumnDimension('A')->setWidth(10);
  219. }
  220. // Loop trough column dimensions
  221. foreach ($pSheet->getColumnDimensions() as $colDimension) {
  222. // col
  223. $objWriter->startElement('col');
  224. $objWriter->writeAttribute('min', PHPExcel_Cell::columnIndexFromString($colDimension->getColumnIndex()));
  225. $objWriter->writeAttribute('max', PHPExcel_Cell::columnIndexFromString($colDimension->getColumnIndex()));
  226. if (!$colDimension->getAutoSize()) {
  227. if ($colDimension->getWidth() < 0) {
  228. // No width set, apply default of 10
  229. $objWriter->writeAttribute('width', '10');
  230. $objWriter->writeAttribute('customWidth', 'true');
  231. } else {
  232. // Width set
  233. $objWriter->writeAttribute('width', $colDimension->getWidth());
  234. $objWriter->writeAttribute('customWidth', 'true');
  235. }
  236. } else {
  237. // Auto size set, calculate maximal column width
  238. $maxWidth = 0;
  239. $tempWidth = 0;
  240. // Loop trough cells
  241. $cellCollection = $pSheet->getCellCollection();
  242. foreach ($cellCollection as $cell) {
  243. if ($cell->getColumn() == $colDimension->getColumnIndex()) {
  244. $tempWidth = PHPExcel_Shared_Font::calculateColumnWidth(
  245. $pSheet->getStyle( strtoupper($cell->getCoordinate()) )->getFont()->getSize(),
  246. false,
  247. $cell->getCalculatedValue()
  248. );
  249. if ($tempWidth > $maxWidth) {
  250. $maxWidth = $tempWidth;
  251. }
  252. }
  253. }
  254. $objWriter->writeAttribute('width', $maxWidth);
  255. $objWriter->writeAttribute('bestFit', 'true');
  256. }
  257. $objWriter->endElement();
  258. }
  259. $objWriter->endElement();
  260. } else {
  261. throw new Exception("Invalid parameters passed.");
  262. }
  263. }
  264. /**
  265. * Write SheetProtection
  266. *
  267. * @param xmlWriter $objWriter XML Writer
  268. * @param PHPExcel_Worksheet $pSheet Worksheet
  269. * @throws Exception
  270. */
  271. private function _writeSheetProtection($objWriter = null, $pSheet = null)
  272. {
  273. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  274. // sheetProtection
  275. $objWriter->startElement('sheetProtection');
  276. if ($pSheet->getProtection()->getPassword() != '') {
  277. $objWriter->writeAttribute('password', $pSheet->getProtection()->getPassword());
  278. }
  279. $objWriter->writeAttribute('sheet', ($pSheet->getProtection()->getSheet() ? 'true' : 'false'));
  280. $objWriter->writeAttribute('objects', ($pSheet->getProtection()->getObjects() ? 'true' : 'false'));
  281. $objWriter->writeAttribute('scenarios', ($pSheet->getProtection()->getScenarios() ? 'true' : 'false'));
  282. $objWriter->writeAttribute('formatCells', ($pSheet->getProtection()->getFormatCells() ? 'true' : 'false'));
  283. $objWriter->writeAttribute('formatColumns', ($pSheet->getProtection()->getFormatColumns() ? 'true' : 'false'));
  284. $objWriter->writeAttribute('formatRows', ($pSheet->getProtection()->getFormatRows() ? 'true' : 'false'));
  285. $objWriter->writeAttribute('insertColumns', ($pSheet->getProtection()->getInsertColumns() ? 'true' : 'false'));
  286. $objWriter->writeAttribute('insertRows', ($pSheet->getProtection()->getInsertRows() ? 'true' : 'false'));
  287. $objWriter->writeAttribute('insertHyperlinks', ($pSheet->getProtection()->getInsertHyperlinks() ? 'true' : 'false'));
  288. $objWriter->writeAttribute('deleteColumns', ($pSheet->getProtection()->getDeleteColumns() ? 'true' : 'false'));
  289. $objWriter->writeAttribute('deleteRows', ($pSheet->getProtection()->getDeleteRows() ? 'true' : 'false'));
  290. $objWriter->writeAttribute('selectLockedCells', ($pSheet->getProtection()->getSelectLockedCells() ? 'true' : 'false'));
  291. $objWriter->writeAttribute('sort', ($pSheet->getProtection()->getSort() ? 'true' : 'false'));
  292. $objWriter->writeAttribute('autoFilter', ($pSheet->getProtection()->getAutoFilter() ? 'true' : 'false'));
  293. $objWriter->writeAttribute('pivotTables', ($pSheet->getProtection()->getPivotTables() ? 'true' : 'false'));
  294. $objWriter->writeAttribute('selectUnlockedCells', ($pSheet->getProtection()->getSelectUnlockedCells() ? 'true' : 'false'));
  295. $objWriter->endElement();
  296. } else {
  297. throw new Exception("Invalid parameters passed.");
  298. }
  299. }
  300. /**
  301. * Write ConditionalFormatting
  302. *
  303. * @param xmlWriter $objWriter XML Writer
  304. * @param PHPExcel_Worksheet $pSheet Worksheet
  305. * @throws Exception
  306. */
  307. private function _writeConditionalFormatting($objWriter = null, $pSheet = null)
  308. {
  309. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  310. // Conditional id
  311. $id = 1;
  312. // Loop trough styles in the current worksheet
  313. foreach ($pSheet->getStyles() as $cellCoodrinate => $style) {
  314. if (count($style->getConditionalStyles()) > 0) {
  315. foreach ($style->getConditionalStyles() as $conditional) {
  316. if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) {
  317. // conditionalFormatting
  318. $objWriter->startElement('conditionalFormatting');
  319. $objWriter->writeAttribute('sqref', $cellCoodrinate);
  320. // cfRule
  321. $objWriter->startElement('cfRule');
  322. $objWriter->writeAttribute('type', $conditional->getConditionType());
  323. $objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ));
  324. $objWriter->writeAttribute('priority', $id++);
  325. if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS
  326. && $conditional->getOperatorType() != PHPExcel_Style_Conditional::OPERATOR_NONE) {
  327. $objWriter->writeAttribute('operator', $conditional->getOperatorType());
  328. // Formula
  329. $objWriter->writeElement('formula', $conditional->getCondition());
  330. }
  331. $objWriter->endElement();
  332. $objWriter->endElement();
  333. }
  334. }
  335. }
  336. }
  337. } else {
  338. throw new Exception("Invalid parameters passed.");
  339. }
  340. }
  341. /**
  342. * Write ProtectedRanges
  343. *
  344. * @param xmlWriter $objWriter XML Writer
  345. * @param PHPExcel_Worksheet $pSheet Worksheet
  346. * @throws Exception
  347. */
  348. private function _writeProtectedRanges($objWriter = null, $pSheet = null)
  349. {
  350. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  351. if (count($pSheet->getProtectedCells()) > 0) {
  352. // protectedRanges
  353. $objWriter->startElement('protectedRanges');
  354. // Loop protectedRanges
  355. foreach ($pSheet->getProtectedCells() as $protectedCell => $passwordHash) {
  356. // protectedRange
  357. $objWriter->startElement('protectedRange');
  358. $objWriter->writeAttribute('name', 'p' . md5($protectedCell));
  359. $objWriter->writeAttribute('sqref', $protectedCell);
  360. $objWriter->writeAttribute('password', $passwordHash);
  361. $objWriter->endElement();
  362. }
  363. $objWriter->endElement();
  364. }
  365. } else {
  366. throw new Exception("Invalid parameters passed.");
  367. }
  368. }
  369. /**
  370. * Write MergeCells
  371. *
  372. * @param xmlWriter $objWriter XML Writer
  373. * @param PHPExcel_Worksheet $pSheet Worksheet
  374. * @throws Exception
  375. */
  376. private function _writeMergeCells($objWriter = null, $pSheet = null)
  377. {
  378. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  379. if (count($pSheet->getMergeCells()) > 0) {
  380. // mergeCells
  381. $objWriter->startElement('mergeCells');
  382. // Loop mergeCells
  383. foreach ($pSheet->getMergeCells() as $mergeCell) {
  384. // mergeCell
  385. $objWriter->startElement('mergeCell');
  386. $objWriter->writeAttribute('ref', $mergeCell);
  387. $objWriter->endElement();
  388. }
  389. $objWriter->endElement();
  390. }
  391. } else {
  392. throw new Exception("Invalid parameters passed.");
  393. }
  394. }
  395. /**
  396. * Write PrintOptions
  397. *
  398. * @param xmlWriter $objWriter XML Writer
  399. * @throws Exception
  400. */
  401. private function _writePrintOptions($objWriter = null)
  402. {
  403. if ($objWriter instanceof xmlWriter) {
  404. // printOptions
  405. $objWriter->writeElement('printOptions', null);
  406. } else {
  407. throw new Exception("Invalid parameters passed.");
  408. }
  409. }
  410. /**
  411. * Write PageMargins
  412. *
  413. * @param xmlWriter $objWriter XML Writer
  414. * @param PHPExcel_Worksheet $pSheet Worksheet
  415. * @throws Exception
  416. */
  417. private function _writePageMargins($objWriter = null, $pSheet = null)
  418. {
  419. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  420. // pageMargins
  421. $objWriter->startElement('pageMargins');
  422. $objWriter->writeAttribute('left', $pSheet->getPageMargins()->getLeft());
  423. $objWriter->writeAttribute('right', $pSheet->getPageMargins()->getRight());
  424. $objWriter->writeAttribute('top', $pSheet->getPageMargins()->getTop());
  425. $objWriter->writeAttribute('bottom', $pSheet->getPageMargins()->getBottom());
  426. $objWriter->writeAttribute('header', $pSheet->getPageMargins()->getHeader());
  427. $objWriter->writeAttribute('footer', $pSheet->getPageMargins()->getFooter());
  428. $objWriter->endElement();
  429. } else {
  430. throw new Exception("Invalid parameters passed.");
  431. }
  432. }
  433. /**
  434. * Write AutoFilter
  435. *
  436. * @param xmlWriter $objWriter XML Writer
  437. * @param PHPExcel_Worksheet $pSheet Worksheet
  438. * @throws Exception
  439. */
  440. private function _writeAutoFilter($objWriter = null, $pSheet = null)
  441. {
  442. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  443. if ($pSheet->getAutoFilter() != '') {
  444. // autoFilter
  445. $objWriter->startElement('autoFilter');
  446. $objWriter->writeAttribute('ref', $pSheet->getAutoFilter());
  447. $objWriter->endElement();
  448. }
  449. } else {
  450. throw new Exception("Invalid parameters passed.");
  451. }
  452. }
  453. /**
  454. * Write PageSetup
  455. *
  456. * @param xmlWriter $objWriter XML Writer
  457. * @param PHPExcel_Worksheet $pSheet Worksheet
  458. * @throws Exception
  459. */
  460. private function _writePageSetup($objWriter = null, $pSheet = null)
  461. {
  462. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  463. // pageSetup
  464. $objWriter->startElement('pageSetup');
  465. $objWriter->writeAttribute('paperSize', $pSheet->getPageSetup()->getPaperSize());
  466. $objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
  467. $objWriter->endElement();
  468. } else {
  469. throw new Exception("Invalid parameters passed.");
  470. }
  471. }
  472. /**
  473. * Write Header / Footer
  474. *
  475. * @param xmlWriter $objWriter XML Writer
  476. * @param PHPExcel_Worksheet $pSheet Worksheet
  477. * @throws Exception
  478. */
  479. private function _writeHeaderFooter($objWriter = null, $pSheet = null)
  480. {
  481. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  482. // headerFooter
  483. $objWriter->startElement('headerFooter');
  484. $objWriter->writeAttribute('differentOddEven', ($pSheet->getHeaderFooter()->getDifferentOddEven() ? 'true' : 'false'));
  485. $objWriter->writeAttribute('differentFirst', ($pSheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
  486. $objWriter->writeAttribute('scaleWithDoc', ($pSheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
  487. $objWriter->writeAttribute('alignWithMargins', ($pSheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
  488. $objWriter->writeElement('oddHeader', $pSheet->getHeaderFooter()->getOddHeader());
  489. $objWriter->writeElement('oddFooter', $pSheet->getHeaderFooter()->getOddFooter());
  490. $objWriter->writeElement('evenHeader', $pSheet->getHeaderFooter()->getEvenHeader());
  491. $objWriter->writeElement('evenFooter', $pSheet->getHeaderFooter()->getEvenFooter());
  492. $objWriter->writeElement('firstHeader', $pSheet->getHeaderFooter()->getFirstHeader());
  493. $objWriter->writeElement('firstFooter', $pSheet->getHeaderFooter()->getFirstFooter());
  494. $objWriter->endElement();
  495. } else {
  496. throw new Exception("Invalid parameters passed.");
  497. }
  498. }
  499. /**
  500. * Write Breaks
  501. *
  502. * @param xmlWriter $objWriter XML Writer
  503. * @param PHPExcel_Worksheet $pSheet Worksheet
  504. * @throws Exception
  505. */
  506. private function _writeBreaks($objWriter = null, $pSheet = null)
  507. {
  508. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  509. // Get row and column breaks
  510. $aRowBreaks = array();
  511. $aColumnBreaks = array();
  512. foreach ($pSheet->getBreaks() as $cell => $breakType) {
  513. if ($breakType == PHPExcel_Worksheet::BREAK_ROW) {
  514. array_push($aRowBreaks, $cell);
  515. } else if ($breakType == PHPExcel_Worksheet::BREAK_COLUMN) {
  516. array_push($aColumnBreaks, $cell);
  517. }
  518. }
  519. // rowBreaks
  520. if (count($aRowBreaks) > 0) {
  521. $objWriter->startElement('rowBreaks');
  522. $objWriter->writeAttribute('count', count($aRowBreaks));
  523. $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
  524. foreach ($aRowBreaks as $cell) {
  525. $coords = PHPExcel_Cell::coordinateFromString($cell);
  526. $objWriter->startElement('brk');
  527. $objWriter->writeAttribute('id', $coords[1]);
  528. $objWriter->writeAttribute('man', '1');
  529. $objWriter->endElement();
  530. }
  531. $objWriter->endElement();
  532. }
  533. // Second, write column breaks
  534. if (count($aColumnBreaks) > 0) {
  535. $objWriter->startElement('colBreaks');
  536. $objWriter->writeAttribute('count', count($aColumnBreaks));
  537. $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
  538. foreach ($aColumnBreaks as $cell) {
  539. $coords = PHPExcel_Cell::coordinateFromString($cell);
  540. $objWriter->startElement('brk');
  541. $objWriter->writeAttribute('id', PHPExcel_Cell::columnIndexFromString($coords[0]) - 1);
  542. $objWriter->writeAttribute('man', '1');
  543. $objWriter->endElement();
  544. }
  545. $objWriter->endElement();
  546. }
  547. } else {
  548. throw new Exception("Invalid parameters passed.");
  549. }
  550. }
  551. /**
  552. * Write SheetData
  553. *
  554. * @param xmlWriter $objWriter XML Writer
  555. * @param PHPExcel_Worksheet $pSheet Worksheet
  556. * @param string[] $pStringTable String table
  557. * @throws Exception
  558. */
  559. private function _writeSheetData($objWriter = null, $pSheet = null, $pStringTable = null)
  560. {
  561. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet && is_array($pStringTable)) {
  562. // Flipped stringtable, for faster index searching
  563. $aFlippedStringTable = array_flip($pStringTable);
  564. // sheetData
  565. $objWriter->startElement('sheetData');
  566. // Get column count
  567. $colCount = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn());
  568. // Loop trough cells
  569. $currentRow = -1;
  570. $cellCollection = $pSheet->getCellCollection();
  571. foreach ($cellCollection as $cell) {
  572. if ($currentRow != $cell->getRow()) {
  573. // End previous row?
  574. if ($currentRow != -1) {
  575. $objWriter->endElement();
  576. }
  577. // Set current row
  578. $currentRow = $cell->getRow();
  579. // Get row dimension
  580. $rowDimension = $pSheet->getRowDimension($currentRow);
  581. // Start a new row
  582. $objWriter->startElement('row');
  583. $objWriter->writeAttribute('r', $currentRow);
  584. $objWriter->writeAttribute('spans', '1:' . $colCount);
  585. // Row dimensions
  586. if ($rowDimension->getRowHeight() >= 0) {
  587. $objWriter->writeAttribute('customHeight', '1');
  588. $objWriter->writeAttribute('ht', $rowDimension->getRowHeight());
  589. }
  590. }
  591. // Write cell
  592. $this->_writeCell($objWriter, $pSheet, $cell, $pStringTable, $aFlippedStringTable);
  593. }
  594. // End last row?
  595. if ($currentRow != -1) {
  596. $objWriter->endElement();
  597. }
  598. $objWriter->endElement();
  599. } else {
  600. throw new Exception("Invalid parameters passed.");
  601. }
  602. }
  603. /**
  604. * Write Cell
  605. *
  606. * @param xmlWriter $objWriter XML Writer
  607. * @param PHPExcel_Worksheet $pSheet Worksheet
  608. * @param PHPExcel_Cell $pCell Cell
  609. * @param string[] $pStringTable String table
  610. * @param string[] $pFlippedStringTable String table (flipped), for faster index searching
  611. * @throws Exception
  612. */
  613. private function _writeCell($objWriter = null, $pSheet = null, $pCell = null, $pStringTable = null, $pFlippedStringTable = null)
  614. {
  615. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet && $pCell instanceof PHPExcel_Cell && is_array($pStringTable) && is_array($pFlippedStringTable)) {
  616. // Cell
  617. $objWriter->startElement('c');
  618. $objWriter->writeAttribute('r', $pCell->getCoordinate());
  619. // Sheet styles
  620. $aStyles = $pSheet->getStyles();
  621. if (isset($aStyles[$pCell->getCoordinate()])) {
  622. $styleIndex = $this->getParentWriter()->getStylesHashTable()->getIndexForHashCode( $aStyles[$pCell->getCoordinate()]->getHashCode() );
  623. if ($styleIndex != '') {
  624. $objWriter->writeAttribute('s', $styleIndex);
  625. }
  626. }
  627. // If cell value is supplied, write cell value
  628. if ($pCell->getValue() != '') {
  629. // Map type
  630. $mappedType = $pCell->getDataType();
  631. // Write data type depending on its type
  632. switch (strtolower($mappedType)) {
  633. case 's': // String
  634. $objWriter->writeAttribute('t', $mappedType);
  635. break;
  636. case 'b': // Boolean
  637. $objWriter->writeAttribute('t', $mappedType);
  638. break;
  639. case 'f': // Formula
  640. $objWriter->writeAttribute('t', 'str');
  641. break;
  642. }
  643. // Write data depending on its type
  644. switch (strtolower($mappedType)) {
  645. case 's': // String
  646. $objWriter->writeElement('v', $pFlippedStringTable[$pCell->getValue()]); //array_search($pCell->getValue(), $pStringTable));
  647. break;
  648. case 'f': // Formula
  649. $objWriter->writeElement('f', substr($pCell->getValue(), 1));
  650. break;
  651. case 'n': // Numeric
  652. $objWriter->writeElement('v', $pCell->getValue());
  653. break;
  654. case 'b': // Boolean
  655. $objWriter->writeElement('v', ($pCell->getValue() ? '1' : '0'));
  656. break;
  657. }
  658. }
  659. $objWriter->endElement();
  660. } else {
  661. throw new Exception("Invalid parameters passed.");
  662. }
  663. }
  664. /**
  665. * Write Drawings
  666. *
  667. * @param xmlWriter $objWriter XML Writer
  668. * @param PHPExcel_Worksheet $pSheet Worksheet
  669. * @throws Exception
  670. */
  671. private function _writeDrawings($objWriter = null, $pSheet = null)
  672. {
  673. if ($objWriter instanceof xmlWriter && $pSheet instanceof PHPExcel_Worksheet) {
  674. // If sheet contains drawings, add the relationships
  675. if ($pSheet->getDrawingCollection()->count() > 0) {
  676. $objWriter->startElement('drawing');
  677. $objWriter->writeAttribute('r:id', 'rId1');
  678. $objWriter->endElement();
  679. }
  680. } else {
  681. throw new Exception("Invalid parameters passed.");
  682. }
  683. }
  684. }