HTML.php 37 KB

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