diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c28392..a2da4900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/references/features-cross-reference.md b/docs/references/features-cross-reference.md index a1b1e012..7ab9d068 100644 --- a/docs/references/features-cross-reference.md +++ b/docs/references/features-cross-reference.md @@ -906,7 +906,7 @@ - + ● diff --git a/samples/46_ReadHtml.php b/samples/46_ReadHtml.php new file mode 100644 index 00000000..838d2868 --- /dev/null +++ b/samples/46_ReadHtml.php @@ -0,0 +1,19 @@ +load($html); + +$helper->logRead('Html', $html, $callStartTime); + +// Save +$helper->write($objPHPExcel, __FILE__); diff --git a/samples/templates/46readHtml.html b/samples/templates/46readHtml.html new file mode 100644 index 00000000..56f745dd --- /dev/null +++ b/samples/templates/46readHtml.html @@ -0,0 +1,130 @@ + + + + + + Competency List + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Color NameHEXColor
Black#000000 
Blue#0000FF 
Green#008000 
Cyan#00FFFF 
Purple#800080 
Grey#808080 
SkyBlue#87CEEB 
Brown#A52A2A 
Silver#C0C0C0 
Chocolate#D2691E 
Tan#D2B48C 
Violet#EE82EE 
Red#FF0000 
Pink#FFC0CB 
Gold#FFD700 
Yellow#FFFF00 
LightYellow#FFFFE0 
White#FFFFFF 
+ + diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 4db753b5..f77ef7a8 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -442,6 +442,9 @@ class Html extends BaseReader implements IReader 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; } @@ -584,4 +587,56 @@ class Html extends BaseReader implements IReader 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; + } + } + } }