PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/application/third_party/PHPExcel/Writer/HTML.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 1377 lines | 715 code | 197 blank | 465 comment | 150 complexity | 4baad64aff7b7fd591b315d4a0d60e60 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 PHPExcel
  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
  23. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Writer_HTML
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter {
  35. /**
  36. * PHPExcel object
  37. *
  38. * @var PHPExcel
  39. */
  40. protected $_phpExcel;
  41. /**
  42. * Sheet index to write
  43. *
  44. * @var int
  45. */
  46. private $_sheetIndex = 0;
  47. /**
  48. * Pre-calculate formulas
  49. *
  50. * @var boolean
  51. */
  52. private $_preCalculateFormulas = true;
  53. /**
  54. * Images root
  55. *
  56. * @var string
  57. */
  58. private $_imagesRoot = '.';
  59. /**
  60. * Use inline CSS?
  61. *
  62. * @var boolean
  63. */
  64. private $_useInlineCss = false;
  65. /**
  66. * Array of CSS styles
  67. *
  68. * @var array
  69. */
  70. private $_cssStyles = null;
  71. /**
  72. * Array of column widths in points
  73. *
  74. * @var array
  75. */
  76. private $_columnWidths = null;
  77. /**
  78. * Default font
  79. *
  80. * @var PHPExcel_Style_Font
  81. */
  82. private $_defaultFont;
  83. /**
  84. * Flag whether spans have been calculated
  85. *
  86. * @var boolean
  87. */
  88. private $_spansAreCalculated = false;
  89. /**
  90. * Excel cells that should not be written as HTML cells
  91. *
  92. * @var array
  93. */
  94. private $_isSpannedCell = array();
  95. /**
  96. * Excel cells that are upper-left corner in a cell merge
  97. *
  98. * @var array
  99. */
  100. private $_isBaseCell = array();
  101. /**
  102. * Excel rows that should not be written as HTML rows
  103. *
  104. * @var array
  105. */
  106. private $_isSpannedRow = array();
  107. /**
  108. * Is the current writer creating PDF?
  109. *
  110. * @var boolean
  111. */
  112. protected $_isPdf = false;
  113. /**
  114. * Generate the Navigation block
  115. *
  116. * @var boolean
  117. */
  118. private $_generateSheetNavigationBlock = true;
  119. /**
  120. * Create a new PHPExcel_Writer_HTML
  121. *
  122. * @param PHPExcel $phpExcel PHPExcel object
  123. */
  124. public function __construct(PHPExcel $phpExcel) {
  125. $this->_phpExcel = $phpExcel;
  126. $this->_defaultFont = $this->_phpExcel->getDefaultStyle()->getFont();
  127. }
  128. /**
  129. * Save PHPExcel to file
  130. *
  131. * @param string $pFilename
  132. * @throws Exception
  133. */
  134. public function save($pFilename = null) {
  135. // garbage collect
  136. $this->_phpExcel->garbageCollect();
  137. $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
  138. PHPExcel_Calculation::getInstance()->writeDebugLog = false;
  139. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  140. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  141. // Build CSS
  142. $this->buildCSS(!$this->_useInlineCss);
  143. // Open file
  144. $fileHandle = fopen($pFilename, 'wb+');
  145. if ($fileHandle === false) {
  146. throw new Exception("Could not open file $pFilename for writing.");
  147. }
  148. // Write headers
  149. fwrite($fileHandle, $this->generateHTMLHeader(!$this->_useInlineCss));
  150. // Write navigation (tabs)
  151. if ((!$this->_isPdf) && ($this->_generateSheetNavigationBlock)) {
  152. fwrite($fileHandle, $this->generateNavigation());
  153. }
  154. // Write data
  155. fwrite($fileHandle, $this->generateSheetData());
  156. // Write footer
  157. fwrite($fileHandle, $this->generateHTMLFooter());
  158. // Close file
  159. fclose($fileHandle);
  160. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  161. PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
  162. }
  163. /**
  164. * Map VAlign
  165. *
  166. * @param string $vAlign Vertical alignment
  167. * @return string
  168. */
  169. private function _mapVAlign($vAlign) {
  170. switch ($vAlign) {
  171. case PHPExcel_Style_Alignment::VERTICAL_BOTTOM: return 'bottom';
  172. case PHPExcel_Style_Alignment::VERTICAL_TOP: return 'top';
  173. case PHPExcel_Style_Alignment::VERTICAL_CENTER:
  174. case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY: return 'middle';
  175. default: return 'baseline';
  176. }
  177. }
  178. /**
  179. * Map HAlign
  180. *
  181. * @param string $hAlign Horizontal alignment
  182. * @return string|false
  183. */
  184. private function _mapHAlign($hAlign) {
  185. switch ($hAlign) {
  186. case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL: return false;
  187. case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: return 'left';
  188. case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT: return 'right';
  189. case PHPExcel_Style_Alignment::HORIZONTAL_CENTER:
  190. case PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS: return 'center';
  191. case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY: return 'justify';
  192. default: return false;
  193. }
  194. }
  195. /**
  196. * Map border style
  197. *
  198. * @param int $borderStyle Sheet index
  199. * @return string
  200. */
  201. private function _mapBorderStyle($borderStyle) {
  202. switch ($borderStyle) {
  203. case PHPExcel_Style_Border::BORDER_NONE: return 'none';
  204. case PHPExcel_Style_Border::BORDER_DASHDOT: return '1px dashed';
  205. case PHPExcel_Style_Border::BORDER_DASHDOTDOT: return '1px dotted';
  206. case PHPExcel_Style_Border::BORDER_DASHED: return '1px dashed';
  207. case PHPExcel_Style_Border::BORDER_DOTTED: return '1px dotted';
  208. case PHPExcel_Style_Border::BORDER_DOUBLE: return '3px double';
  209. case PHPExcel_Style_Border::BORDER_HAIR: return '1px solid';
  210. case PHPExcel_Style_Border::BORDER_MEDIUM: return '2px solid';
  211. case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT: return '2px dashed';
  212. case PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT: return '2px dotted';
  213. case PHPExcel_Style_Border::BORDER_MEDIUMDASHED: return '2px dashed';
  214. case PHPExcel_Style_Border::BORDER_SLANTDASHDOT: return '2px dashed';
  215. case PHPExcel_Style_Border::BORDER_THICK: return '3px solid';
  216. case PHPExcel_Style_Border::BORDER_THIN: return '1px solid';
  217. default: return '1px solid'; // map others to thin
  218. }
  219. }
  220. /**
  221. * Get sheet index
  222. *
  223. * @return int
  224. */
  225. public function getSheetIndex() {
  226. return $this->_sheetIndex;
  227. }
  228. /**
  229. * Set sheet index
  230. *
  231. * @param int $pValue Sheet index
  232. * @return PHPExcel_Writer_HTML
  233. */
  234. public function setSheetIndex($pValue = 0) {
  235. $this->_sheetIndex = $pValue;
  236. return $this;
  237. }
  238. /**
  239. * Get sheet index
  240. *
  241. * @return boolean
  242. */
  243. public function getGenerateSheetNavigationBlock() {
  244. return $this->_generateSheetNavigationBlock;
  245. }
  246. /**
  247. * Set sheet index
  248. *
  249. * @param boolean $pValue Flag indicating whether the sheet navigation block should be generated or not
  250. * @return PHPExcel_Writer_HTML
  251. */
  252. public function setGenerateSheetNavigationBlock($pValue = true) {
  253. $this->_generateSheetNavigationBlock = (bool) $pValue;
  254. return $this;
  255. }
  256. /**
  257. * Write all sheets (resets sheetIndex to NULL)
  258. */
  259. public function writeAllSheets() {
  260. $this->_sheetIndex = null;
  261. return $this;
  262. }
  263. /**
  264. * Generate HTML header
  265. *
  266. * @param boolean $pIncludeStyles Include styles?
  267. * @return string
  268. * @throws Exception
  269. */
  270. public function generateHTMLHeader($pIncludeStyles = false) {
  271. // PHPExcel object known?
  272. if (is_null($this->_phpExcel)) {
  273. throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  274. }
  275. // Construct HTML
  276. $properties = $this->_phpExcel->getProperties();
  277. $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' . PHP_EOL;
  278. $html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' . PHP_EOL;
  279. $html .= '<html>' . PHP_EOL;
  280. $html .= ' <head>' . PHP_EOL;
  281. $html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . PHP_EOL;
  282. if ($properties->getTitle() > '')
  283. $html .= ' <title>' . htmlspecialchars($properties->getTitle()) . '</title>' . PHP_EOL;
  284. if ($properties->getCreator() > '')
  285. $html .= ' <meta name="author" content="' . htmlspecialchars($properties->getCreator()) . '" />' . PHP_EOL;
  286. if ($properties->getTitle() > '')
  287. $html .= ' <meta name="title" content="' . htmlspecialchars($properties->getTitle()) . '" />' . PHP_EOL;
  288. if ($properties->getDescription() > '')
  289. $html .= ' <meta name="description" content="' . htmlspecialchars($properties->getDescription()) . '" />' . PHP_EOL;
  290. if ($properties->getSubject() > '')
  291. $html .= ' <meta name="subject" content="' . htmlspecialchars($properties->getSubject()) . '" />' . PHP_EOL;
  292. if ($properties->getKeywords() > '')
  293. $html .= ' <meta name="keywords" content="' . htmlspecialchars($properties->getKeywords()) . '" />' . PHP_EOL;
  294. if ($properties->getCategory() > '')
  295. $html .= ' <meta name="category" content="' . htmlspecialchars($properties->getCategory()) . '" />' . PHP_EOL;
  296. if ($properties->getCompany() > '')
  297. $html .= ' <meta name="company" content="' . htmlspecialchars($properties->getCompany()) . '" />' . PHP_EOL;
  298. if ($properties->getManager() > '')
  299. $html .= ' <meta name="manager" content="' . htmlspecialchars($properties->getManager()) . '" />' . PHP_EOL;
  300. if ($pIncludeStyles) {
  301. $html .= $this->generateStyles(true);
  302. }
  303. $html .= ' </head>' . PHP_EOL;
  304. $html .= '' . PHP_EOL;
  305. $html .= ' <body>' . PHP_EOL;
  306. // Return
  307. return $html;
  308. }
  309. /**
  310. * Generate sheet data
  311. *
  312. * @return string
  313. * @throws Exception
  314. */
  315. public function generateSheetData() {
  316. // PHPExcel object known?
  317. if (is_null($this->_phpExcel)) {
  318. throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  319. }
  320. // Ensure that Spans have been calculated?
  321. if (!$this->_spansAreCalculated) {
  322. $this->_calculateSpans();
  323. }
  324. // Fetch sheets
  325. $sheets = array();
  326. if (is_null($this->_sheetIndex)) {
  327. $sheets = $this->_phpExcel->getAllSheets();
  328. } else {
  329. $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  330. }
  331. // Construct HTML
  332. $html = '';
  333. // Loop all sheets
  334. $sheetId = 0;
  335. foreach ($sheets as $sheet) {
  336. // Write table header
  337. $html .= $this->_generateTableHeader($sheet);
  338. // Get worksheet dimension
  339. $dimension = explode(':', $sheet->calculateWorksheetDimension());
  340. $dimension[0] = PHPExcel_Cell::coordinateFromString($dimension[0]);
  341. $dimension[0][0] = PHPExcel_Cell::columnIndexFromString($dimension[0][0]) - 1;
  342. $dimension[1] = PHPExcel_Cell::coordinateFromString($dimension[1]);
  343. $dimension[1][0] = PHPExcel_Cell::columnIndexFromString($dimension[1][0]) - 1;
  344. // row min,max
  345. $rowMin = $dimension[0][1];
  346. $rowMax = $dimension[1][1];
  347. // calculate start of <tbody>, <thead>
  348. $tbodyStart = $rowMin;
  349. $tbodyEnd = $rowMax;
  350. $theadStart = $theadEnd = 0; // default: no <thead> no </thead>
  351. if ($sheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  352. $rowsToRepeatAtTop = $sheet->getPageSetup()->getRowsToRepeatAtTop();
  353. // we can only support repeating rows that start at top row
  354. if ($rowsToRepeatAtTop[0] == 1) {
  355. $theadStart = $rowsToRepeatAtTop[0];
  356. $theadEnd = $rowsToRepeatAtTop[1];
  357. $tbodyStart = $rowsToRepeatAtTop[1] + 1;
  358. }
  359. }
  360. // Loop through cells
  361. $row = $rowMin-1;
  362. while($row++ < $rowMax) {
  363. // <thead> ?
  364. if ($row == $theadStart) {
  365. $html .= ' <thead>' . PHP_EOL;
  366. }
  367. // <tbody> ?
  368. if ($row == $tbodyStart) {
  369. $html .= ' <tbody>' . PHP_EOL;
  370. }
  371. // Write row if there are HTML table cells in it
  372. if ( !isset($this->_isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row]) ) {
  373. // Start a new rowData
  374. $rowData = array();
  375. // Loop through columns
  376. $column = $dimension[0][0] - 1;
  377. while($column++ < $dimension[1][0]) {
  378. // Cell exists?
  379. if ($sheet->cellExistsByColumnAndRow($column, $row)) {
  380. $rowData[$column] = $sheet->getCellByColumnAndRow($column, $row);
  381. } else {
  382. $rowData[$column] = '';
  383. }
  384. }
  385. $html .= $this->_generateRow($sheet, $rowData, $row - 1);
  386. }
  387. // </thead> ?
  388. if ($row == $theadEnd) {
  389. $html .= ' </thead>' . PHP_EOL;
  390. }
  391. // </tbody> ?
  392. if ($row == $tbodyEnd) {
  393. $html .= ' </tbody>' . PHP_EOL;
  394. }
  395. }
  396. // Write table footer
  397. $html .= $this->_generateTableFooter();
  398. // Writing PDF?
  399. if ($this->_isPdf) {
  400. if (is_null($this->_sheetIndex) && $sheetId + 1 < $this->_phpExcel->getSheetCount()) {
  401. $html .= '<div style="page-break-before:always" />';
  402. }
  403. }
  404. // Next sheet
  405. ++$sheetId;
  406. }
  407. // Return
  408. return $html;
  409. }
  410. /**
  411. * Generate sheet tabs
  412. *
  413. * @return string
  414. * @throws Exception
  415. */
  416. public function generateNavigation()
  417. {
  418. // PHPExcel object known?
  419. if (is_null($this->_phpExcel)) {
  420. throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  421. }
  422. // Fetch sheets
  423. $sheets = array();
  424. if (is_null($this->_sheetIndex)) {
  425. $sheets = $this->_phpExcel->getAllSheets();
  426. } else {
  427. $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  428. }
  429. // Construct HTML
  430. $html = '';
  431. // Only if there are more than 1 sheets
  432. if (count($sheets) > 1) {
  433. // Loop all sheets
  434. $sheetId = 0;
  435. $html .= '<ul class="navigation">' . PHP_EOL;
  436. foreach ($sheets as $sheet) {
  437. $html .= ' <li class="sheet' . $sheetId . '"><a href="#sheet' . $sheetId . '">' . $sheet->getTitle() . '</a></li>' . PHP_EOL;
  438. ++$sheetId;
  439. }
  440. $html .= '</ul>' . PHP_EOL;
  441. }
  442. return $html;
  443. }
  444. /**
  445. * Generate image tag in cell
  446. *
  447. * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
  448. * @param string $coordinates Cell coordinates
  449. * @return string
  450. * @throws Exception
  451. */
  452. private function _writeImageTagInCell(PHPExcel_Worksheet $pSheet, $coordinates) {
  453. // Construct HTML
  454. $html = '';
  455. // Write images
  456. foreach ($pSheet->getDrawingCollection() as $drawing) {
  457. if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
  458. if ($drawing->getCoordinates() == $coordinates) {
  459. $filename = $drawing->getPath();
  460. // Strip off eventual '.'
  461. if (substr($filename, 0, 1) == '.') {
  462. $filename = substr($filename, 1);
  463. }
  464. // Prepend images root
  465. $filename = $this->getImagesRoot() . $filename;
  466. // Strip off eventual '.'
  467. if (substr($filename, 0, 1) == '.' && substr($filename, 0, 2) != './') {
  468. $filename = substr($filename, 1);
  469. }
  470. // Convert UTF8 data to PCDATA
  471. $filename = htmlspecialchars($filename);
  472. $html .= PHP_EOL;
  473. $html .= ' <img style="position: relative; left: ' . $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' . $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' . $filename . '" border="0" width="' . $drawing->getWidth() . '" height="' . $drawing->getHeight() . '" />' . PHP_EOL;
  474. }
  475. }
  476. }
  477. // Return
  478. return $html;
  479. }
  480. /**
  481. * Generate CSS styles
  482. *
  483. * @param boolean $generateSurroundingHTML Generate surrounding HTML tags? (<style> and </style>)
  484. * @return string
  485. * @throws Exception
  486. */
  487. public function generateStyles($generateSurroundingHTML = true) {
  488. // PHPExcel object known?
  489. if (is_null($this->_phpExcel)) {
  490. throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  491. }
  492. // Build CSS
  493. $css = $this->buildCSS($generateSurroundingHTML);
  494. // Construct HTML
  495. $html = '';
  496. // Start styles
  497. if ($generateSurroundingHTML) {
  498. $html .= ' <style type="text/css">' . PHP_EOL;
  499. $html .= ' html { ' . $this->_assembleCSS($css['html']) . ' }' . PHP_EOL;
  500. }
  501. // Write all other styles
  502. foreach ($css as $styleName => $styleDefinition) {
  503. if ($styleName != 'html') {
  504. $html .= ' ' . $styleName . ' { ' . $this->_assembleCSS($styleDefinition) . ' }' . PHP_EOL;
  505. }
  506. }
  507. // End styles
  508. if ($generateSurroundingHTML) {
  509. $html .= ' </style>' . PHP_EOL;
  510. }
  511. // Return
  512. return $html;
  513. }
  514. /**
  515. * Build CSS styles
  516. *
  517. * @param boolean $generateSurroundingHTML Generate surrounding HTML style? (html { })
  518. * @return array
  519. * @throws Exception
  520. */
  521. public function buildCSS($generateSurroundingHTML = true) {
  522. // PHPExcel object known?
  523. if (is_null($this->_phpExcel)) {
  524. throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  525. }
  526. // Cached?
  527. if (!is_null($this->_cssStyles)) {
  528. return $this->_cssStyles;
  529. }
  530. // Ensure that spans have been calculated
  531. if (!$this->_spansAreCalculated) {
  532. $this->_calculateSpans();
  533. }
  534. // Construct CSS
  535. $css = array();
  536. // Start styles
  537. if ($generateSurroundingHTML) {
  538. // html { }
  539. $css['html']['font-family'] = 'Calibri, Arial, Helvetica, sans-serif';
  540. $css['html']['font-size'] = '11pt';
  541. $css['html']['background-color'] = 'white';
  542. }
  543. // table { }
  544. $css['table']['border-collapse'] = 'collapse';
  545. if (!$this->_isPdf) {
  546. $css['table']['page-break-after'] = 'always';
  547. }
  548. // .gridlines td { }
  549. $css['.gridlines td']['border'] = '1px dotted black';
  550. // .b {}
  551. $css['.b']['text-align'] = 'center'; // BOOL
  552. // .e {}
  553. $css['.e']['text-align'] = 'center'; // ERROR
  554. // .f {}
  555. $css['.f']['text-align'] = 'right'; // FORMULA
  556. // .inlineStr {}
  557. $css['.inlineStr']['text-align'] = 'left'; // INLINE
  558. // .n {}
  559. $css['.n']['text-align'] = 'right'; // NUMERIC
  560. // .s {}
  561. $css['.s']['text-align'] = 'left'; // STRING
  562. // Calculate cell style hashes
  563. foreach ($this->_phpExcel->getCellXfCollection() as $index => $style) {
  564. $css['td.style' . $index] = $this->_createCSSStyle( $style );
  565. }
  566. // Fetch sheets
  567. $sheets = array();
  568. if (is_null($this->_sheetIndex)) {
  569. $sheets = $this->_phpExcel->getAllSheets();
  570. } else {
  571. $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  572. }
  573. // Build styles per sheet
  574. foreach ($sheets as $sheet) {
  575. // Calculate hash code
  576. $sheetIndex = $sheet->getParent()->getIndex($sheet);
  577. // Build styles
  578. // Calculate column widths
  579. $sheet->calculateColumnWidths();
  580. // col elements, initialize
  581. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()) - 1;
  582. $column = -1;
  583. while($column++ < $highestColumnIndex) {
  584. $this->_columnWidths[$sheetIndex][$column] = 42; // approximation
  585. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = '42pt';
  586. }
  587. // col elements, loop through columnDimensions and set width
  588. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  589. if (($width = PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth(), $this->_defaultFont)) >= 0) {
  590. $width = PHPExcel_Shared_Drawing::pixelsToPoints($width);
  591. $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
  592. $this->_columnWidths[$sheetIndex][$column] = $width;
  593. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt';
  594. if ($columnDimension->getVisible() === false) {
  595. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse';
  596. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['*display'] = 'none'; // target IE6+7
  597. }
  598. }
  599. }
  600. // Default row height
  601. $rowDimension = $sheet->getDefaultRowDimension();
  602. // table.sheetN tr { }
  603. $css['table.sheet' . $sheetIndex . ' tr'] = array();
  604. if ($rowDimension->getRowHeight() == -1) {
  605. $pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
  606. } else {
  607. $pt_height = $rowDimension->getRowHeight();
  608. }
  609. $css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt';
  610. if ($rowDimension->getVisible() === false) {
  611. $css['table.sheet' . $sheetIndex . ' tr']['display'] = 'none';
  612. $css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden';
  613. }
  614. // Calculate row heights
  615. foreach ($sheet->getRowDimensions() as $rowDimension) {
  616. $row = $rowDimension->getRowIndex() - 1;
  617. // table.sheetN tr.rowYYYYYY { }
  618. $css['table.sheet' . $sheetIndex . ' tr.row' . $row] = array();
  619. if ($rowDimension->getRowHeight() == -1) {
  620. $pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
  621. } else {
  622. $pt_height = $rowDimension->getRowHeight();
  623. }
  624. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt';
  625. if ($rowDimension->getVisible() === false) {
  626. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none';
  627. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden';
  628. }
  629. }
  630. }
  631. // Cache
  632. if (is_null($this->_cssStyles)) {
  633. $this->_cssStyles = $css;
  634. }
  635. // Return
  636. return $css;
  637. }
  638. /**
  639. * Create CSS style
  640. *
  641. * @param PHPExcel_Style $pStyle PHPExcel_Style
  642. * @return array
  643. */
  644. private function _createCSSStyle(PHPExcel_Style $pStyle) {
  645. // Construct CSS
  646. $css = '';
  647. // Create CSS
  648. $css = array_merge(
  649. $this->_createCSSStyleAlignment($pStyle->getAlignment())
  650. , $this->_createCSSStyleBorders($pStyle->getBorders())
  651. , $this->_createCSSStyleFont($pStyle->getFont())
  652. , $this->_createCSSStyleFill($pStyle->getFill())
  653. );
  654. // Return
  655. return $css;
  656. }
  657. /**
  658. * Create CSS style (PHPExcel_Style_Alignment)
  659. *
  660. * @param PHPExcel_Style_Alignment $pStyle PHPExcel_Style_Alignment
  661. * @return array
  662. */
  663. private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle) {
  664. // Construct CSS
  665. $css = array();
  666. // Create CSS
  667. $css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
  668. if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) {
  669. $css['text-align'] = $textAlign;
  670. if(in_array($textAlign,array('left','right')))
  671. $css['padding-'.$textAlign] = (string)((int)$pStyle->getIndent() * 9).'px';
  672. }
  673. // Return
  674. return $css;
  675. }
  676. /**
  677. * Create CSS style (PHPExcel_Style_Font)
  678. *
  679. * @param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
  680. * @return array
  681. */
  682. private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle) {
  683. // Construct CSS
  684. $css = array();
  685. // Create CSS
  686. if ($pStyle->getBold()) {
  687. $css['font-weight'] = 'bold';
  688. }
  689. if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
  690. $css['text-decoration'] = 'underline line-through';
  691. } else if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
  692. $css['text-decoration'] = 'underline';
  693. } else if ($pStyle->getStrikethrough()) {
  694. $css['text-decoration'] = 'line-through';
  695. }
  696. if ($pStyle->getItalic()) {
  697. $css['font-style'] = 'italic';
  698. }
  699. $css['color'] = '#' . $pStyle->getColor()->getRGB();
  700. $css['font-family'] = '\'' . $pStyle->getName() . '\'';
  701. $css['font-size'] = $pStyle->getSize() . 'pt';
  702. // Return
  703. return $css;
  704. }
  705. /**
  706. * Create CSS style (PHPExcel_Style_Borders)
  707. *
  708. * @param PHPExcel_Style_Borders $pStyle PHPExcel_Style_Borders
  709. * @return array
  710. */
  711. private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle) {
  712. // Construct CSS
  713. $css = array();
  714. // Create CSS
  715. $css['border-bottom'] = $this->_createCSSStyleBorder($pStyle->getBottom());
  716. $css['border-top'] = $this->_createCSSStyleBorder($pStyle->getTop());
  717. $css['border-left'] = $this->_createCSSStyleBorder($pStyle->getLeft());
  718. $css['border-right'] = $this->_createCSSStyleBorder($pStyle->getRight());
  719. // Return
  720. return $css;
  721. }
  722. /**
  723. * Create CSS style (PHPExcel_Style_Border)
  724. *
  725. * @param PHPExcel_Style_Border $pStyle PHPExcel_Style_Border
  726. * @return string
  727. */
  728. private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle) {
  729. // Create CSS
  730. $css = $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
  731. // Return
  732. return $css;
  733. }
  734. /**
  735. * Create CSS style (PHPExcel_Style_Fill)
  736. *
  737. * @param PHPExcel_Style_Fill $pStyle PHPExcel_Style_Fill
  738. * @return array
  739. */
  740. private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle) {
  741. // Construct HTML
  742. $css = array();
  743. // Create CSS
  744. $value = $pStyle->getFillType() == PHPExcel_Style_Fill::FILL_NONE ?
  745. 'white' : '#' . $pStyle->getStartColor()->getRGB();
  746. $css['background-color'] = $value;
  747. // Return
  748. return $css;
  749. }
  750. /**
  751. * Generate HTML footer
  752. */
  753. public function generateHTMLFooter() {
  754. // Construct HTML
  755. $html = '';
  756. $html .= ' </body>' . PHP_EOL;
  757. $html .= '</html>' . PHP_EOL;
  758. // Return
  759. return $html;
  760. }
  761. /**
  762. * Generate table header
  763. *
  764. * @param PHPExcel_Worksheet $pSheet The worksheet for the table we are writing
  765. * @return string
  766. * @throws Exception
  767. */
  768. private function _generateTableHeader($pSheet) {
  769. $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
  770. // Construct HTML
  771. $html = '';
  772. if (!$this->_useInlineCss) {
  773. $gridlines = $pSheet->getShowGridLines() ? ' gridlines' : '';
  774. $html .= ' <table border="0" cellpadding="0" cellspacing="0" id="sheet' . $sheetIndex . '" class="sheet' . $sheetIndex . $gridlines . '">' . PHP_EOL;
  775. } else {
  776. $style = isset($this->_cssStyles['table']) ?
  777. $this->_assembleCSS($this->_cssStyles['table']) : '';
  778. if ($this->_isPdf && $pSheet->getShowGridLines()) {
  779. $html .= ' <table border="1" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="4" style="' . $style . '">' . PHP_EOL;
  780. } else {
  781. $html .= ' <table border="0" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="4" style="' . $style . '">' . PHP_EOL;
  782. }
  783. }
  784. // Write <col> elements
  785. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn()) - 1;
  786. $i = -1;
  787. while($i++ < $highestColumnIndex) {
  788. if (!$this->_isPdf) {
  789. if (!$this->_useInlineCss) {
  790. $html .= ' <col class="col' . $i . '">' . PHP_EOL;
  791. } else {
  792. $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) ?
  793. $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) : '';
  794. $html .= ' <col style="' . $style . '">' . PHP_EOL;
  795. }
  796. }
  797. }
  798. // Return
  799. return $html;
  800. }
  801. /**
  802. * Generate table footer
  803. *
  804. * @throws Exception
  805. */
  806. private function _generateTableFooter() {
  807. // Construct HTML
  808. $html = '';
  809. $html .= ' </table>' . PHP_EOL;
  810. // Return
  811. return $html;
  812. }
  813. /**
  814. * Generate row
  815. *
  816. * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
  817. * @param array $pValues Array containing cells in a row
  818. * @param int $pRow Row number (0-based)
  819. * @return string
  820. * @throws Exception
  821. */
  822. private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
  823. if (is_array($pValues)) {
  824. // Construct HTML
  825. $html = '';
  826. // Sheet index
  827. $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
  828. // DomPDF and breaks
  829. if ($this->_isPdf && count($pSheet->getBreaks()) > 0) {
  830. $breaks = $pSheet->getBreaks();
  831. // check if a break is needed before this row
  832. if (isset($breaks['A' . $pRow])) {
  833. // close table: </table>
  834. $html .= $this->_generateTableFooter();
  835. // insert page break
  836. $html .= '<div style="page-break-before:always" />';
  837. // open table again: <table> + <col> etc.
  838. $html .= $this->_generateTableHeader($pSheet);
  839. }
  840. }
  841. // Write row start
  842. if (!$this->_useInlineCss) {
  843. $html .= ' <tr class="row' . $pRow . '">' . PHP_EOL;
  844. } else {
  845. $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow])
  846. ? $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
  847. $html .= ' <tr style="' . $style . '">' . PHP_EOL;
  848. }
  849. // Write cells
  850. $colNum = 0;
  851. foreach ($pValues as $cell) {
  852. $coordinate = PHPExcel_Cell::stringFromColumnIndex($colNum) . ($pRow + 1);
  853. if (!$this->_useInlineCss) {
  854. $cssClass = '';
  855. $cssClass = 'column' . $colNum;
  856. } else {
  857. $cssClass = array();
  858. if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
  859. $this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
  860. }
  861. }
  862. $colSpan = 1;
  863. $rowSpan = 1;
  864. // initialize
  865. $cellData = '';
  866. // PHPExcel_Cell
  867. if ($cell instanceof PHPExcel_Cell) {
  868. if (is_null($cell->getParent())) {
  869. $cell->attach($pSheet);
  870. }
  871. // Value
  872. if ($cell->getValue() instanceof PHPExcel_RichText) {
  873. // Loop through rich text elements
  874. $elements = $cell->getValue()->getRichTextElements();
  875. foreach ($elements as $element) {
  876. // Rich text start?
  877. if ($element instanceof PHPExcel_RichText_Run) {
  878. $cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';
  879. if ($element->getFont()->getSuperScript()) {
  880. $cellData .= '<sup>';
  881. } else if ($element->getFont()->getSubScript()) {
  882. $cellData .= '<sub>';
  883. }
  884. }
  885. // Convert UTF8 data to PCDATA
  886. $cellText = $element->getText();
  887. $cellData .= htmlspecialchars($cellText);
  888. if ($element instanceof PHPExcel_RichText_Run) {
  889. if ($element->getFont()->getSuperScript()) {
  890. $cellData .= '</sup>';
  891. } else if ($element->getFont()->getSubScript()) {
  892. $cellData .= '</sub>';
  893. }
  894. $cellData .= '</span>';
  895. }
  896. }
  897. } else {
  898. if ($this->_preCalculateFormulas) {
  899. $cellData = PHPExcel_Style_NumberFormat::toFormattedString(
  900. $cell->getCalculatedValue(),
  901. $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
  902. array($this, 'formatColor')
  903. );
  904. } else {
  905. $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
  906. $cell->getValue(),
  907. $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
  908. array($this, 'formatColor')
  909. );
  910. }
  911. $cellData = htmlspecialchars($cellData);
  912. if ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSuperScript()) {
  913. $cellData = '<sup>'.$cellData.'</sup>';
  914. } elseif ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSubScript()) {
  915. $cellData = '<sub>'.$cellData.'</sub>';
  916. }
  917. }
  918. // Converts the cell content so that spaces occuring at beginning of each new line are replaced by &nbsp;
  919. // Example: " Hello\n to the world" is converted to "&nbsp;&nbsp;Hello\n&nbsp;to the world"
  920. $cellData = preg_replace("/(?m)(?:^|\\G) /", '&nbsp;', $cellData);
  921. // convert newline "\n" to '<br>'
  922. $cellData = nl2br($cellData);
  923. // Extend CSS class?
  924. if (!$this->_useInlineCss) {
  925. $cssClass .= ' style' . $cell->getXfIndex();
  926. $cssClass .= ' ' . $cell->getDataType();
  927. } else {
  928. if (isset($this->_cssStyles['td.style' . $cell->getXfIndex()])) {
  929. $cssClass = array_merge($cssClass, $this->_cssStyles['td.style' . $cell->getXfIndex()]);
  930. }
  931. // General horizontal alignment: Actual horizontal alignment depends on dataType
  932. $sharedStyle = $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() );
  933. if ($sharedStyle->getAlignment()->getHorizontal() == PHPExcel_Style_Alignment::HORIZONTAL_GENERAL
  934. && isset($this->_cssStyles['.' . $cell->getDataType()]['text-align']))
  935. {
  936. $cssClass['text-align'] = $this->_cssStyles['.' . $cell->getDataType()]['text-align'];
  937. }
  938. }
  939. }
  940. // Hyperlink?
  941. if ($pSheet->hyperlinkExists($coordinate) && !$pSheet->getHyperlink($coordinate)->isInternal()) {
  942. $cellData = '<a href="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getUrl()) . '" title="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getTooltip()) . '">' . $cellData . '</a>';
  943. }
  944. // Should the cell be written or is it swallowed by a rowspan or colspan?
  945. $writeCell = ! ( isset($this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])
  946. && $this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum] );
  947. // Colspan and Rowspan
  948. $colspan = 1;
  949. $rowspan = 1;
  950. if (isset($this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])) {
  951. $spans = $this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum];
  952. $rowSpan = $spans['rowspan'];
  953. $colSpan = $spans['colspan'];
  954. }
  955. // Write
  956. if ($writeCell) {
  957. // Column start
  958. $html .= ' <td';
  959. if (!$this->_useInlineCss) {
  960. $html .= ' class="' . $cssClass . '"';
  961. } else {
  962. //** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
  963. // We must explicitly write the width of the <td> element because TCPDF
  964. // does not recognize e.g. <col style="width:42pt">
  965. $width = 0;
  966. $i = $colNum - 1;
  967. $e = $colNum + $colSpan - 1;
  968. while($i++ < $e) {
  969. if (isset($this->_columnWidths[$sheetIndex][$i])) {
  970. $width += $this->_columnWidths[$sheetIndex][$i];
  971. }
  972. }
  973. $cssClass['width'] = $width . 'pt';
  974. // We must also explicitly write the height of the <td> element because TCPDF
  975. // does not recognize e.g. <tr style="height:50pt">
  976. if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
  977. $height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
  978. $cssClass['height'] = $height;
  979. }
  980. //** end of redundant code **
  981. $html .= ' style="' . $this->_assembleCSS($cssClass) . '"';
  982. }
  983. if ($colSpan > 1) {
  984. $html .= ' colspan="' . $colSpan . '"';
  985. }
  986. if ($rowSpan > 1) {
  987. $html .= ' rowspan="' . $rowSpan . '"';
  988. }
  989. $html .= '>';
  990. // Image?
  991. $html .= $this->_writeImageTagInCell($pSheet, $coordinate);
  992. // Cell data
  993. $html .= $cellData;
  994. // Column end
  995. $html .= '</td>' . PHP_EOL;
  996. }
  997. // Next column
  998. ++$colNum;
  999. }
  1000. // Write row end
  1001. $html .= ' </tr>' . PHP_EOL;
  1002. // Return
  1003. return $html;
  1004. } else {
  1005. throw new Exception("Invalid parameters passed.");
  1006. }
  1007. }
  1008. /**
  1009. * Takes array where of CSS properties / values and converts to CSS string
  1010. *
  1011. * @param array
  1012. * @return string
  1013. */
  1014. private function _assembleCSS($pValue = array())
  1015. {
  1016. $pairs = array();
  1017. foreach ($pValue as $property => $value) {
  1018. $pairs[] = $property . ':' . $value;
  1019. }
  1020. $string = implode('; ', $pairs);
  1021. return $string;
  1022. }
  1023. /**
  1024. * Get Pre-Calculate Formulas
  1025. *
  1026. * @return boolean
  1027. */
  1028. public function getPreCalculateFormulas() {
  1029. return $this->_preCalculateFormulas;
  1030. }
  1031. /**
  1032. * Set Pre-Calculate Formulas
  1033. *
  1034. * @param boolean $pValue Pre-Calculate Formulas?
  1035. * @return PHPExcel_Writer_HTML
  1036. */
  1037. public function setPreCalculateFormulas($pValue = true) {
  1038. $this->_preCalculateFormulas = $pValue;
  1039. return $this;
  1040. }
  1041. /**
  1042. * Get images root
  1043. *
  1044. * @return string
  1045. */
  1046. public function getImagesRoot() {
  1047. return $this->_imagesRoot;
  1048. }
  1049. /**
  1050. * Set images root
  1051. *
  1052. * @param string $pValue
  1053. * @return PHPExcel_Writer_HTML
  1054. */
  1055. public function setImagesRoot($pValue = '.') {
  1056. $this->_imagesRoot = $pValue;
  1057. return $this;
  1058. }
  1059. /**
  1060. * Get use inline CSS?
  1061. *
  1062. * @return boolean
  1063. */
  1064. public function getUseInlineCss() {
  1065. return $this->_useInlineCss;
  1066. }
  1067. /**
  1068. * Set use inline CSS?
  1069. *
  1070. * @param boolean $pValue
  1071. * @return PHPExcel_Writer_HTML
  1072. */
  1073. public function setUseInlineCss($pValue = false) {
  1074. $this->_useInlineCss = $pValue;
  1075. return $this;
  1076. }
  1077. /**
  1078. * Add color to formatted string as inline style
  1079. *
  1080. * @param string $pValue Plain formatted value without color
  1081. * @param string $pFormat Format code
  1082. * @return string
  1083. */
  1084. public function formatColor($pValue, $pFormat)
  1085. {
  1086. // Color information, e.g. [Red] is always at the beginning
  1087. $color = null; // initialize
  1088. $matches = array();
  1089. $color_regex = '/^\\[[a-zA-Z]+\\]/';
  1090. if (preg_match($color_regex, $pFormat, $matches)) {
  1091. $color = str_replace('[', '', $matches[0]);
  1092. $color = str_replace(']', '', $color);
  1093. $color = strtolower($color);
  1094. }
  1095. // convert to PCDATA
  1096. $value = htmlspecialchars($pValue);
  1097. // color span tag
  1098. if ($color !== null) {
  1099. $value = '<span style="color:' . $color . '">' . $value . '</span>';
  1100. }
  1101. return $value;
  1102. }
  1103. /**
  1104. * Calculate information about HTML colspan and rowspan which is not always the same as Excel's
  1105. */
  1106. private function _calculateSpans()
  1107. {
  1108. // Identify all cells that should be omitted in HTML due to cell merge.
  1109. // In HTML only the upper-left cell should be written and it should have
  1110. // appropriate rowspan / colspan attribute
  1111. $sheetIndexes = $this->_sheetIndex !== null ?
  1112. array($this->_sheetIndex) : range(0, $this->_phpExcel->getSheetCount() - 1);
  1113. foreach ($sheetIndexes as $sheetIndex) {
  1114. $sheet = $this->_phpExcel->getSheet($sheetIndex);
  1115. $candidateSpannedRow = array();
  1116. // loop through all Excel merged cells
  1117. foreach ($sheet->getMergeCells() as $cells) {
  1118. list($cells, ) = PHPExcel_Cell::splitRange($cells);
  1119. $first = $cells[0];
  1120. $last = $cells[1];
  1121. list($fc, $fr) = PHPExcel_Cell::coordinateFromString($first);
  1122. $fc = PHPExcel_Cell::columnIndexFromString($fc) - 1;
  1123. list($lc, $lr) = PHPExcel_Cell::coordinateFromString($last);
  1124. $lc = PHPExcel_Cell::columnIndexFromString($lc) - 1;
  1125. // loop through the individual cells in the individual merge
  1126. $r = $fr - 1;
  1127. while($r++ < $lr) {
  1128. // also, flag this row as a HTML row that is candidate to be omitted
  1129. $candidateSpannedRow[$r] = $r;
  1130. $c = $fc - 1;
  1131. while($c++ < $lc) {
  1132. if ( !($c == $fc && $r == $fr) ) {
  1133. // not the upper-left cell (should not be written in HTML)
  1134. $this->_isSpannedCell[$sheetIndex][$r][$c] = array(
  1135. 'baseCell' => array($fr, $fc),
  1136. );
  1137. } else {
  1138. // upper-left is the base cell that should hold the colspan/rowspan attribute
  1139. $this->_isBaseCell[$sheetIndex][$r][$c] = array(
  1140. 'xlrowspan' => $lr - $fr + 1, // Excel rowspan
  1141. 'rowspan' => $lr - $fr + 1, // HTML rowspan, value may change
  1142. 'xlcolspan' => $lc - $fc + 1, // Excel colspan
  1143. 'colspan' => $lc - $fc + 1, // HTML colspan, value may change
  1144. );
  1145. }
  1146. }
  1147. }
  1148. }
  1149. // Identify which rows should be omitted in HTML. These are the rows where all the cells
  1150. // participate in a merge and the where base cells are somewhere above.
  1151. $countColumns = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
  1152. foreach ($candidateSpannedRow as $rowIndex) {
  1153. if (isset($this->_isSpannedCell[$sheetIndex][$rowIndex])) {
  1154. if (count($this->_isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) {
  1155. $this->_isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex;
  1156. };
  1157. }
  1158. }
  1159. // For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
  1160. if ( isset($this->_isSpannedRow[$sheetIndex]) ) {
  1161. foreach ($this->_isSpannedRow[$sheetIndex] as $rowIndex) {
  1162. $adjustedBaseCells = array();
  1163. $c = -1;
  1164. $e = $countColumns - 1;
  1165. while($c++ < $e) {
  1166. $baseCell = $this->_isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell'];
  1167. if ( !in_array($baseCell, $adjustedBaseCells) ) {
  1168. // subtract rowspan by 1
  1169. --$this->_isBaseCell[$sheetIndex][ $baseCell[0] ][ $baseCell[1] ]['rowspan'];
  1170. $adjustedBaseCells[] = $baseCell;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. // TODO: Same for columns
  1176. }
  1177. // We have calculated the spans
  1178. $this->_spansAreCalculated = true;
  1179. }
  1180. }