PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/include/PHPExcel/Writer/HTML.php

https://bitbucket.org/sleininger/stock_online
PHP | 1370 lines | 708 code | 197 blank | 465 comment | 147 complexity | 87defb5065d74f072f7bd0e135eff52b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.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 1.7.7, 2012-05-19
  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 '1px hidden';
  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. $css['table']['page-break-after'] = 'always';
  546. // .gridlines td { }
  547. $css['.gridlines td']['border'] = '1px dotted black';
  548. // .b {}
  549. $css['.b']['text-align'] = 'center'; // BOOL
  550. // .e {}
  551. $css['.e']['text-align'] = 'center'; // ERROR
  552. // .f {}
  553. $css['.f']['text-align'] = 'right'; // FORMULA
  554. // .inlineStr {}
  555. $css['.inlineStr']['text-align'] = 'left'; // INLINE
  556. // .n {}
  557. $css['.n']['text-align'] = 'right'; // NUMERIC
  558. // .s {}
  559. $css['.s']['text-align'] = 'left'; // STRING
  560. // Calculate cell style hashes
  561. foreach ($this->_phpExcel->getCellXfCollection() as $index => $style) {
  562. $css['td.style' . $index] = $this->_createCSSStyle( $style );
  563. }
  564. // Fetch sheets
  565. $sheets = array();
  566. if (is_null($this->_sheetIndex)) {
  567. $sheets = $this->_phpExcel->getAllSheets();
  568. } else {
  569. $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  570. }
  571. // Build styles per sheet
  572. foreach ($sheets as $sheet) {
  573. // Calculate hash code
  574. $sheetIndex = $sheet->getParent()->getIndex($sheet);
  575. // Build styles
  576. // Calculate column widths
  577. $sheet->calculateColumnWidths();
  578. // col elements, initialize
  579. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()) - 1;
  580. $column = -1;
  581. while($column++ < $highestColumnIndex) {
  582. $this->_columnWidths[$sheetIndex][$column] = 42; // approximation
  583. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = '42pt';
  584. }
  585. // col elements, loop through columnDimensions and set width
  586. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  587. if (($width = PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth(), $this->_defaultFont)) >= 0) {
  588. $width = PHPExcel_Shared_Drawing::pixelsToPoints($width);
  589. $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
  590. $this->_columnWidths[$sheetIndex][$column] = $width;
  591. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt';
  592. if ($columnDimension->getVisible() === false) {
  593. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse';
  594. $css['table.sheet' . $sheetIndex . ' col.col' . $column]['*display'] = 'none'; // target IE6+7
  595. }
  596. }
  597. }
  598. // Default row height
  599. $rowDimension = $sheet->getDefaultRowDimension();
  600. // table.sheetN tr { }
  601. $css['table.sheet' . $sheetIndex . ' tr'] = array();
  602. if ($rowDimension->getRowHeight() == -1) {
  603. $pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
  604. } else {
  605. $pt_height = $rowDimension->getRowHeight();
  606. }
  607. $css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt';
  608. if ($rowDimension->getVisible() === false) {
  609. $css['table.sheet' . $sheetIndex . ' tr']['display'] = 'none';
  610. $css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden';
  611. }
  612. // Calculate row heights
  613. foreach ($sheet->getRowDimensions() as $rowDimension) {
  614. $row = $rowDimension->getRowIndex() - 1;
  615. // table.sheetN tr.rowYYYYYY { }
  616. $css['table.sheet' . $sheetIndex . ' tr.row' . $row] = array();
  617. if ($rowDimension->getRowHeight() == -1) {
  618. $pt_height = PHPExcel_Shared_Font::getDefaultRowHeightByFont($this->_phpExcel->getDefaultStyle()->getFont());
  619. } else {
  620. $pt_height = $rowDimension->getRowHeight();
  621. }
  622. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt';
  623. if ($rowDimension->getVisible() === false) {
  624. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none';
  625. $css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden';
  626. }
  627. }
  628. }
  629. // Cache
  630. if (is_null($this->_cssStyles)) {
  631. $this->_cssStyles = $css;
  632. }
  633. // Return
  634. return $css;
  635. }
  636. /**
  637. * Create CSS style
  638. *
  639. * @param PHPExcel_Style $pStyle PHPExcel_Style
  640. * @return array
  641. */
  642. private function _createCSSStyle(PHPExcel_Style $pStyle) {
  643. // Construct CSS
  644. $css = '';
  645. // Create CSS
  646. $css = array_merge(
  647. $this->_createCSSStyleAlignment($pStyle->getAlignment())
  648. , $this->_createCSSStyleBorders($pStyle->getBorders())
  649. , $this->_createCSSStyleFont($pStyle->getFont())
  650. , $this->_createCSSStyleFill($pStyle->getFill())
  651. );
  652. // Return
  653. return $css;
  654. }
  655. /**
  656. * Create CSS style (PHPExcel_Style_Alignment)
  657. *
  658. * @param PHPExcel_Style_Alignment $pStyle PHPExcel_Style_Alignment
  659. * @return array
  660. */
  661. private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle) {
  662. // Construct CSS
  663. $css = array();
  664. // Create CSS
  665. $css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
  666. if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) {
  667. $css['text-align'] = $textAlign;
  668. }
  669. // Return
  670. return $css;
  671. }
  672. /**
  673. * Create CSS style (PHPExcel_Style_Font)
  674. *
  675. * @param PHPExcel_Style_Font $pStyle PHPExcel_Style_Font
  676. * @return array
  677. */
  678. private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle) {
  679. // Construct CSS
  680. $css = array();
  681. // Create CSS
  682. if ($pStyle->getBold()) {
  683. $css['font-weight'] = 'bold';
  684. }
  685. if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
  686. $css['text-decoration'] = 'underline line-through';
  687. } else if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
  688. $css['text-decoration'] = 'underline';
  689. } else if ($pStyle->getStrikethrough()) {
  690. $css['text-decoration'] = 'line-through';
  691. }
  692. if ($pStyle->getItalic()) {
  693. $css['font-style'] = 'italic';
  694. }
  695. $css['color'] = '#' . $pStyle->getColor()->getRGB();
  696. $css['font-family'] = '\'' . $pStyle->getName() . '\'';
  697. $css['font-size'] = $pStyle->getSize() . 'pt';
  698. // Return
  699. return $css;
  700. }
  701. /**
  702. * Create CSS style (PHPExcel_Style_Borders)
  703. *
  704. * @param PHPExcel_Style_Borders $pStyle PHPExcel_Style_Borders
  705. * @return array
  706. */
  707. private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle) {
  708. // Construct CSS
  709. $css = array();
  710. // Create CSS
  711. $css['border-bottom'] = $this->_createCSSStyleBorder($pStyle->getBottom());
  712. $css['border-top'] = $this->_createCSSStyleBorder($pStyle->getTop());
  713. $css['border-left'] = $this->_createCSSStyleBorder($pStyle->getLeft());
  714. $css['border-right'] = $this->_createCSSStyleBorder($pStyle->getRight());
  715. // Return
  716. return $css;
  717. }
  718. /**
  719. * Create CSS style (PHPExcel_Style_Border)
  720. *
  721. * @param PHPExcel_Style_Border $pStyle PHPExcel_Style_Border
  722. * @return string
  723. */
  724. private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle) {
  725. // Create CSS
  726. $css = $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
  727. // Return
  728. return $css;
  729. }
  730. /**
  731. * Create CSS style (PHPExcel_Style_Fill)
  732. *
  733. * @param PHPExcel_Style_Fill $pStyle PHPExcel_Style_Fill
  734. * @return array
  735. */
  736. private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle) {
  737. // Construct HTML
  738. $css = array();
  739. // Create CSS
  740. $value = $pStyle->getFillType() == PHPExcel_Style_Fill::FILL_NONE ?
  741. 'white' : '#' . $pStyle->getStartColor()->getRGB();
  742. $css['background-color'] = $value;
  743. // Return
  744. return $css;
  745. }
  746. /**
  747. * Generate HTML footer
  748. */
  749. public function generateHTMLFooter() {
  750. // Construct HTML
  751. $html = '';
  752. $html .= ' </body>' . PHP_EOL;
  753. $html .= '</html>' . PHP_EOL;
  754. // Return
  755. return $html;
  756. }
  757. /**
  758. * Generate table header
  759. *
  760. * @param PHPExcel_Worksheet $pSheet The worksheet for the table we are writing
  761. * @return string
  762. * @throws Exception
  763. */
  764. private function _generateTableHeader($pSheet) {
  765. $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
  766. // Construct HTML
  767. $html = '';
  768. if (!$this->_useInlineCss) {
  769. $gridlines = $pSheet->getShowGridLines() ? ' gridlines' : '';
  770. $html .= ' <table border="0" cellpadding="0" cellspacing="0" id="sheet' . $sheetIndex . '" class="sheet' . $sheetIndex . $gridlines . '">' . PHP_EOL;
  771. } else {
  772. $style = isset($this->_cssStyles['table']) ?
  773. $this->_assembleCSS($this->_cssStyles['table']) : '';
  774. if ($this->_isPdf && $pSheet->getShowGridLines()) {
  775. $html .= ' <table border="1" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="4" style="' . $style . '">' . PHP_EOL;
  776. } else {
  777. $html .= ' <table border="0" cellpadding="1" id="sheet' . $sheetIndex . '" cellspacing="4" style="' . $style . '">' . PHP_EOL;
  778. }
  779. }
  780. // Write <col> elements
  781. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn()) - 1;
  782. $i = -1;
  783. while($i++ < $highestColumnIndex) {
  784. if (!$this->_useInlineCss) {
  785. $html .= ' <col class="col' . $i . '">' . PHP_EOL;
  786. } else {
  787. $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) ?
  788. $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) : '';
  789. $html .= ' <col style="' . $style . '">' . PHP_EOL;
  790. }
  791. }
  792. // Return
  793. return $html;
  794. }
  795. /**
  796. * Generate table footer
  797. *
  798. * @throws Exception
  799. */
  800. private function _generateTableFooter() {
  801. // Construct HTML
  802. $html = '';
  803. $html .= ' </table>' . PHP_EOL;
  804. // Return
  805. return $html;
  806. }
  807. /**
  808. * Generate row
  809. *
  810. * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet
  811. * @param array $pValues Array containing cells in a row
  812. * @param int $pRow Row number (0-based)
  813. * @return string
  814. * @throws Exception
  815. */
  816. private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
  817. if (is_array($pValues)) {
  818. // Construct HTML
  819. $html = '';
  820. // Sheet index
  821. $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
  822. // DomPDF and breaks
  823. if ($this->_isPdf && count($pSheet->getBreaks()) > 0) {
  824. $breaks = $pSheet->getBreaks();
  825. // check if a break is needed before this row
  826. if (isset($breaks['A' . $pRow])) {
  827. // close table: </table>
  828. $html .= $this->_generateTableFooter();
  829. // insert page break
  830. $html .= '<div style="page-break-before:always" />';
  831. // open table again: <table> + <col> etc.
  832. $html .= $this->_generateTableHeader($pSheet);
  833. }
  834. }
  835. // Write row start
  836. if (!$this->_useInlineCss) {
  837. $html .= ' <tr class="row' . $pRow . '">' . PHP_EOL;
  838. } else {
  839. $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow])
  840. ? $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
  841. $html .= ' <tr style="' . $style . '">' . PHP_EOL;
  842. }
  843. // Write cells
  844. $colNum = 0;
  845. foreach ($pValues as $cell) {
  846. $coordinate = PHPExcel_Cell::stringFromColumnIndex($colNum) . ($pRow + 1);
  847. if (!$this->_useInlineCss) {
  848. $cssClass = '';
  849. $cssClass = 'column' . $colNum;
  850. } else {
  851. $cssClass = array();
  852. if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
  853. $this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
  854. }
  855. }
  856. $colSpan = 1;
  857. $rowSpan = 1;
  858. // initialize
  859. $cellData = '';
  860. // PHPExcel_Cell
  861. if ($cell instanceof PHPExcel_Cell) {
  862. if (is_null($cell->getParent())) {
  863. $cell->attach($pSheet);
  864. }
  865. // Value
  866. if ($cell->getValue() instanceof PHPExcel_RichText) {
  867. // Loop through rich text elements
  868. $elements = $cell->getValue()->getRichTextElements();
  869. foreach ($elements as $element) {
  870. // Rich text start?
  871. if ($element instanceof PHPExcel_RichText_Run) {
  872. $cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';
  873. if ($element->getFont()->getSuperScript()) {
  874. $cellData .= '<sup>';
  875. } else if ($element->getFont()->getSubScript()) {
  876. $cellData .= '<sub>';
  877. }
  878. }
  879. // Convert UTF8 data to PCDATA
  880. $cellText = $element->getText();
  881. $cellData .= htmlspecialchars($cellText);
  882. if ($element instanceof PHPExcel_RichText_Run) {
  883. if ($element->getFont()->getSuperScript()) {
  884. $cellData .= '</sup>';
  885. } else if ($element->getFont()->getSubScript()) {
  886. $cellData .= '</sub>';
  887. }
  888. $cellData .= '</span>';
  889. }
  890. }
  891. } else {
  892. if ($this->_preCalculateFormulas) {
  893. $cellData = PHPExcel_Style_NumberFormat::toFormattedString(
  894. $cell->getCalculatedValue(),
  895. $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
  896. array($this, 'formatColor')
  897. );
  898. } else {
  899. $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
  900. $cell->getValue(),
  901. $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode(),
  902. array($this, 'formatColor')
  903. );
  904. }
  905. if ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSuperScript()) {
  906. $cellData = '<sup>'.$cellData.'</sup>';
  907. } elseif ($pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() )->getFont()->getSubScript()) {
  908. $cellData = '<sub>'.$cellData.'</sub>';
  909. }
  910. }
  911. // Converts the cell content so that spaces occuring at beginning of each new line are replaced by &nbsp;
  912. // Example: " Hello\n to the world" is converted to "&nbsp;&nbsp;Hello\n&nbsp;to the world"
  913. $cellData = preg_replace("/(?m)(?:^|\\G) /", '&nbsp;', $cellData);
  914. // convert newline "\n" to '<br>'
  915. $cellData = nl2br($cellData);
  916. // Extend CSS class?
  917. if (!$this->_useInlineCss) {
  918. $cssClass .= ' style' . $cell->getXfIndex();
  919. $cssClass .= ' ' . $cell->getDataType();
  920. } else {
  921. if (isset($this->_cssStyles['td.style' . $cell->getXfIndex()])) {
  922. $cssClass = array_merge($cssClass, $this->_cssStyles['td.style' . $cell->getXfIndex()]);
  923. }
  924. // General horizontal alignment: Actual horizontal alignment depends on dataType
  925. $sharedStyle = $pSheet->getParent()->getCellXfByIndex( $cell->getXfIndex() );
  926. if ($sharedStyle->getAlignment()->getHorizontal() == PHPExcel_Style_Alignment::HORIZONTAL_GENERAL
  927. && isset($this->_cssStyles['.' . $cell->getDataType()]['text-align']))
  928. {
  929. $cssClass['text-align'] = $this->_cssStyles['.' . $cell->getDataType()]['text-align'];
  930. }
  931. }
  932. }
  933. // Hyperlink?
  934. if ($pSheet->hyperlinkExists($coordinate) && !$pSheet->getHyperlink($coordinate)->isInternal()) {
  935. $cellData = '<a href="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getUrl()) . '" title="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getTooltip()) . '">' . $cellData . '</a>';
  936. }
  937. // Should the cell be written or is it swallowed by a rowspan or colspan?
  938. $writeCell = ! ( isset($this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])
  939. && $this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum] );
  940. // Colspan and Rowspan
  941. $colspan = 1;
  942. $rowspan = 1;
  943. if (isset($this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])) {
  944. $spans = $this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum];
  945. $rowSpan = $spans['rowspan'];
  946. $colSpan = $spans['colspan'];
  947. }
  948. // Write
  949. if ($writeCell) {
  950. // Column start
  951. $html .= ' <td';
  952. if (!$this->_useInlineCss) {
  953. $html .= ' class="' . $cssClass . '"';
  954. } else {
  955. //** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
  956. // We must explicitly write the width of the <td> element because TCPDF
  957. // does not recognize e.g. <col style="width:42pt">
  958. $width = 0;
  959. $i = $colNum - 1;
  960. $e = $colNum + $colSpan - 1;
  961. while($i++ < $e) {
  962. if (isset($this->_columnWidths[$sheetIndex][$i])) {
  963. $width += $this->_columnWidths[$sheetIndex][$i];
  964. }
  965. }
  966. $cssClass['width'] = $width . 'pt';
  967. // We must also explicitly write the height of the <td> element because TCPDF
  968. // does not recognize e.g. <tr style="height:50pt">
  969. if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
  970. $height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
  971. $cssClass['height'] = $height;
  972. }
  973. //** end of redundant code **
  974. $html .= ' style="' . $this->_assembleCSS($cssClass) . '"';
  975. }
  976. if ($colSpan > 1) {
  977. $html .= ' colspan="' . $colSpan . '"';
  978. }
  979. if ($rowSpan > 1) {
  980. $html .= ' rowspan="' . $rowSpan . '"';
  981. }
  982. $html .= '>';
  983. // Image?
  984. $html .= $this->_writeImageTagInCell($pSheet, $coordinate);
  985. // Cell data
  986. $html .= $cellData;
  987. // Column end
  988. $html .= '</td>' . PHP_EOL;
  989. }
  990. // Next column
  991. ++$colNum;
  992. }
  993. // Write row end
  994. $html .= ' </tr>' . PHP_EOL;
  995. // Return
  996. return $html;
  997. } else {
  998. throw new Exception("Invalid parameters passed.");
  999. }
  1000. }
  1001. /**
  1002. * Takes array where of CSS properties / values and converts to CSS string
  1003. *
  1004. * @param array
  1005. * @return string
  1006. */
  1007. private function _assembleCSS($pValue = array())
  1008. {
  1009. $pairs = array();
  1010. foreach ($pValue as $property => $value) {
  1011. $pairs[] = $property . ':' . $value;
  1012. }
  1013. $string = implode('; ', $pairs);
  1014. return $string;
  1015. }
  1016. /**
  1017. * Get Pre-Calculate Formulas
  1018. *
  1019. * @return boolean
  1020. */
  1021. public function getPreCalculateFormulas() {
  1022. return $this->_preCalculateFormulas;
  1023. }
  1024. /**
  1025. * Set Pre-Calculate Formulas
  1026. *
  1027. * @param boolean $pValue Pre-Calculate Formulas?
  1028. * @return PHPExcel_Writer_HTML
  1029. */
  1030. public function setPreCalculateFormulas($pValue = true) {
  1031. $this->_preCalculateFormulas = $pValue;
  1032. return $this;
  1033. }
  1034. /**
  1035. * Get images root
  1036. *
  1037. * @return string
  1038. */
  1039. public function getImagesRoot() {
  1040. return $this->_imagesRoot;
  1041. }
  1042. /**
  1043. * Set images root
  1044. *
  1045. * @param string $pValue
  1046. * @return PHPExcel_Writer_HTML
  1047. */
  1048. public function setImagesRoot($pValue = '.') {
  1049. $this->_imagesRoot = $pValue;
  1050. return $this;
  1051. }
  1052. /**
  1053. * Get use inline CSS?
  1054. *
  1055. * @return boolean
  1056. */
  1057. public function getUseInlineCss() {
  1058. return $this->_useInlineCss;
  1059. }
  1060. /**
  1061. * Set use inline CSS?
  1062. *
  1063. * @param boolean $pValue
  1064. * @return PHPExcel_Writer_HTML
  1065. */
  1066. public function setUseInlineCss($pValue = false) {
  1067. $this->_useInlineCss = $pValue;
  1068. return $this;
  1069. }
  1070. /**
  1071. * Add color to formatted string as inline style
  1072. *
  1073. * @param string $pValue Plain formatted value without color
  1074. * @param string $pFormat Format code
  1075. * @return string
  1076. */
  1077. public function formatColor($pValue, $pFormat)
  1078. {
  1079. // Color information, e.g. [Red] is always at the beginning
  1080. $color = null; // initialize
  1081. $matches = array();
  1082. $color_regex = '/^\\[[a-zA-Z]+\\]/';
  1083. if (preg_match($color_regex, $pFormat, $matches)) {
  1084. $color = str_replace('[', '', $matches[0]);
  1085. $color = str_replace(']', '', $color);
  1086. $color = strtolower($color);
  1087. }
  1088. // convert to PCDATA
  1089. $value = htmlspecialchars($pValue);
  1090. // color span tag
  1091. if ($color !== null) {
  1092. $value = '<span style="color:' . $color . '">' . $value . '</span>';
  1093. }
  1094. return $value;
  1095. }
  1096. /**
  1097. * Calculate information about HTML colspan and rowspan which is not always the same as Excel's
  1098. */
  1099. private function _calculateSpans()
  1100. {
  1101. // Identify all cells that should be omitted in HTML due to cell merge.
  1102. // In HTML only the upper-left cell should be written and it should have
  1103. // appropriate rowspan / colspan attribute
  1104. $sheetIndexes = $this->_sheetIndex !== null ?
  1105. array($this->_sheetIndex) : range(0, $this->_phpExcel->getSheetCount() - 1);
  1106. foreach ($sheetIndexes as $sheetIndex) {
  1107. $sheet = $this->_phpExcel->getSheet($sheetIndex);
  1108. $candidateSpannedRow = array();
  1109. // loop through all Excel merged cells
  1110. foreach ($sheet->getMergeCells() as $cells) {
  1111. list($cells, ) = PHPExcel_Cell::splitRange($cells);
  1112. $first = $cells[0];
  1113. $last = $cells[1];
  1114. list($fc, $fr) = PHPExcel_Cell::coordinateFromString($first);
  1115. $fc = PHPExcel_Cell::columnIndexFromString($fc) - 1;
  1116. list($lc, $lr) = PHPExcel_Cell::coordinateFromString($last);
  1117. $lc = PHPExcel_Cell::columnIndexFromString($lc) - 1;
  1118. // loop through the individual cells in the individual merge
  1119. $r = $fr - 1;
  1120. while($r++ < $lr) {
  1121. // also, flag this row as a HTML row that is candidate to be omitted
  1122. $candidateSpannedRow[$r] = $r;
  1123. $c = $fc - 1;
  1124. while($c++ < $lc) {
  1125. if ( !($c == $fc && $r == $fr) ) {
  1126. // not the upper-left cell (should not be written in HTML)
  1127. $this->_isSpannedCell[$sheetIndex][$r][$c] = array(
  1128. 'baseCell' => array($fr, $fc),
  1129. );
  1130. } else {
  1131. // upper-left is the base cell that should hold the colspan/rowspan attribute
  1132. $this->_isBaseCell[$sheetIndex][$r][$c] = array(
  1133. 'xlrowspan' => $lr - $fr + 1, // Excel rowspan
  1134. 'rowspan' => $lr - $fr + 1, // HTML rowspan, value may change
  1135. 'xlcolspan' => $lc - $fc + 1, // Excel colspan
  1136. 'colspan' => $lc - $fc + 1, // HTML colspan, value may change
  1137. );
  1138. }
  1139. }
  1140. }
  1141. }
  1142. // Identify which rows should be omitted in HTML. These are the rows where all the cells
  1143. // participate in a merge and the where base cells are somewhere above.
  1144. $countColumns = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
  1145. foreach ($candidateSpannedRow as $rowIndex) {
  1146. if (isset($this->_isSpannedCell[$sheetIndex][$rowIndex])) {
  1147. if (count($this->_isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) {
  1148. $this->_isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex;
  1149. };
  1150. }
  1151. }
  1152. // For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
  1153. if ( isset($this->_isSpannedRow[$sheetIndex]) ) {
  1154. foreach ($this->_isSpannedRow[$sheetIndex] as $rowIndex) {
  1155. $adjustedBaseCells = array();
  1156. $c = -1;
  1157. $e = $countColumns - 1;
  1158. while($c++ < $e) {
  1159. $baseCell = $this->_isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell'];
  1160. if ( !in_array($baseCell, $adjustedBaseCells) ) {
  1161. // subtract rowspan by 1
  1162. --$this->_isBaseCell[$sheetIndex][ $baseCell[0] ][ $baseCell[1] ]['rowspan'];
  1163. $adjustedBaseCells[] = $baseCell;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. // TODO: Same for columns
  1169. }
  1170. // We have calculated the spans
  1171. $this->_spansAreCalculated = true;
  1172. }
  1173. }