HTML reader supports text and background color from inline CSS, for `td` and `th` element

Closes #180
This commit is contained in:
Kifni Taufik Darmawan 2017-06-16 14:36:27 +07:00 committed by Adrien Crivelli
parent 6aae764920
commit 2a6ab1776c
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
5 changed files with 206 additions and 1 deletions

View File

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Additional codepages - Additional codepages
- MemoryDrawing not working in HTML writer [#808](https://github.com/PHPOffice/PHPExcel/issues/808) - 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) - 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 ### Changed

View File

@ -906,7 +906,7 @@
<td></td> <td></td>
<td></td> <td></td>
<td></td> <td></td>
<td></td> <td style="text-align: center; color: orange;"></td>
<td></td> <td></td>
<td></td> <td></td>
<td></td> <td></td>

19
samples/46_ReadHtml.php Normal file
View File

@ -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__);

View File

@ -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>

View File

@ -442,6 +442,9 @@ class Html extends BaseReader implements IReader
case 'td': case 'td':
$this->processDomElement($child, $sheet, $row, $column, $cellContent); $this->processDomElement($child, $sheet, $row, $column, $cellContent);
// apply inline style
$this->applyInlineStyle($sheet, $row, $column, $attributeArray);
while (isset($this->rowspan[$column . $row])) { while (isset($this->rowspan[$column . $row])) {
++$column; ++$column;
} }
@ -584,4 +587,56 @@ class Html extends BaseReader implements IReader
return $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;
}
}
}
} }