Skip to content

Commit

Permalink
HTML reader supports text and background color from inline CSS, for `…
Browse files Browse the repository at this point in the history
…td` and `th` element

Closes #180
  • Loading branch information
kifni41 authored and PowerKiKi committed Jun 23, 2017
1 parent 6aae764 commit 2a6ab17
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Additional codepages
- MemoryDrawing not working in HTML writer [#808](https://github.com/PHPOffice/PHPExcel/issues/808)
- CSV Reader can auto-detect the separator used in file [#141](https://github.com/PHPOffice/PhpSpreadsheet/pull/141)
- HTML Reader supports some basic inline styles [#180](https://github.com/PHPOffice/PhpSpreadsheet/pull/180)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion docs/references/features-cross-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@
<td></td>
<td></td>
<td></td>
<td></td>
<td style="text-align: center; color: orange;">●</td>
<td></td>
<td></td>
<td></td>
Expand Down
19 changes: 19 additions & 0 deletions samples/46_ReadHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

// Turn off error reporting
error_reporting(0);

use PhpOffice\PhpSpreadsheet\IOFactory;

require __DIR__ . '/Header.php';

$html = __DIR__ . '/templates/46readHtml.html';
$callStartTime = microtime(true);

$objReader = IOFactory::createReader('Html');
$objPHPExcel = $objReader->load($html);

$helper->logRead('Html', $html, $callStartTime);

// Save
$helper->write($objPHPExcel, __FILE__);
130 changes: 130 additions & 0 deletions samples/templates/46readHtml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Competency List</title>
</head>
<body>
<table class="w3-table-all notranslate">
<tr>
<th style="background-color: #0000FF;color:#FFFFFF">Color Name</th>
<th style="background-color: #FF0000">HEX</th>
<th style="background-color: #008000">Color</th>
</tr>

<tr>
<td style="color:#000000">Black</td>
<td>#000000</td>
<td style="background-color:#000000">&nbsp;</td>
</tr>

<tr>
<td style="color:#0000FF">Blue</td>
<td>#0000FF</td>
<td style="background-color:#0000FF">&nbsp;</td>
</tr>


<tr>
<td style="color:#008000">Green</td>
<td>#008000</td>
<td style="background-color:#008000">&nbsp;</td>
</tr>

<tr>
<td style="color:#00FFFF">Cyan</td>
<td>#00FFFF</td>
<td style="background-color:#00FFFF">&nbsp;</td>
</tr>

<tr>
<td style="color:#800080">Purple</td>
<td>#800080</td>
<td style="background-color:#800080">&nbsp;</td>
</tr>


<tr>
<td style="color:#800080">Grey</td>
<td>#808080</td>
<td style="background-color:#808080">&nbsp;</td>
</tr>

<tr>
<td style="color:#87CEEB">SkyBlue</td>
<td>#87CEEB</td>
<td style="background-color:#87CEEB">&nbsp;</td>
</tr>


<tr>
<td style="color:#A52A2A">Brown</td>
<td>#A52A2A</td>
<td style="background-color:#A52A2A">&nbsp;</td>
</tr>

<tr>
<td style="color:#C0C0C0;background-color: #000000">Silver</td>
<td>#C0C0C0</td>
<td style="background-color:#C0C0C0">&nbsp;</td>
</tr>

<tr>
<td style="color:#D2691E">Chocolate</td>
<td>#D2691E</td>
<td style="background-color:#D2691E">&nbsp;</td>
</tr>

<tr>
<td style="color:#D2B48C;background-color: #000000">Tan</td>
<td>#D2B48C</td>
<td style="background-color:#D2B48C">&nbsp;</td>
</tr>


<tr>
<td style="color:#EE82EE;background-color: #000000">Violet</td>
<td>#EE82EE</td>
<td style="background-color:#EE82EE">&nbsp;</td>
</tr>


<tr>
<td style="color:#FF0000;background-color: #000000">Red</td>
<td>#FF0000</td>
<td style="background-color:#FF0000">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFC0CB;background-color: #000000">Pink</td>
<td>#FFC0CB</td>
<td style="background-color:#FFC0CB">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFD700;background-color: #000000">Gold</td>
<td>#FFD700</td>
<td style="background-color:#FFD700">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFF00;background-color: #000000">Yellow</td>
<td>#FFFF00</td>
<td style="background-color:#FFFF00">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFFE0;background-color: #000000">LightYellow</td>
<td>#FFFFE0</td>
<td style="background-color:#FFFFE0">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFFFF;background-color: #000000">White</td>
<td>#FFFFFF</td>
<td style="background-color:#FFFFFF">&nbsp;</td>
</tr>
</table>
</body>
</html>
55 changes: 55 additions & 0 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row,
case 'td':
$this->processDomElement($child, $sheet, $row, $column, $cellContent);

// apply inline style
$this->applyInlineStyle($sheet, $row, $column, $attributeArray);

while (isset($this->rowspan[$column . $row])) {
++$column;
}
Expand Down Expand Up @@ -584,4 +587,56 @@ public function securityScan($xml)

return $xml;
}

/**
* Apply inline css inline style.
*
* NOTES :
* Currently only intended for td & th element,
* and only takes 'background-color' and 'color'; property with HEX color
*
* TODO :
* - Implement to other propertie, such as border
*
* @param Worksheet $sheet
* @param array $attributeArray
* @param int $row
* @param string $column
*/
private function applyInlineStyle(&$sheet, $row, $column, $attributeArray)
{
if (!isset($attributeArray['style'])) {
return;
}

$supported_styles = ['background-color', 'color'];

// add color styles (background & text) from dom element,currently support : td & th, using ONLY inline css style with RGB color
$styles = explode(';', $attributeArray['style']);
foreach ($styles as $st) {
$value = explode(':', $st);

if (empty(trim($value[0])) || !in_array(trim($value[0]), $supported_styles)) {
continue;
}

//check if has #, so we can get clean hex
if (substr(trim($value[1]), 0, 1) == '#') {
$style_color = substr(trim($value[1]), 1);
}

if (empty($style_color)) {
continue;
}

switch (trim($value[0])) {
case 'background-color':
$sheet->getStyle($column . $row)->applyFromArray(['fill' => ['type' => Fill::FILL_SOLID, 'color' => ['rgb' => "{$style_color}"]]]);
break;
case 'color':
$sheet->getStyle($column . $row)->applyFromArray(['font' => ['color' => ['rgb' => "$style_color}"]]]);
break;
}
}
}
}

0 comments on commit 2a6ab17

Please sign in to comment.