From 2a6ab1776c8f31bcb84d2354be0c60d1b70b74a5 Mon Sep 17 00:00:00 2001 From: Kifni Taufik Darmawan Date: Fri, 16 Jun 2017 14:36:27 +0700 Subject: [PATCH] HTML reader supports text and background color from inline CSS, for `td` and `th` element Closes #180 --- CHANGELOG.md | 1 + docs/references/features-cross-reference.md | 2 +- samples/46_ReadHtml.php | 19 +++ samples/templates/46readHtml.html | 130 ++++++++++++++++++++ src/PhpSpreadsheet/Reader/Html.php | 55 +++++++++ 5 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 samples/46_ReadHtml.php create mode 100644 samples/templates/46readHtml.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 30c2839236..a2da490075 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 a1b1e012d0..7ab9d068c6 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 0000000000..838d286822 --- /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 0000000000..56f745dd86 --- /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 4db753b559..f77ef7a813 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -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; } @@ -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; + } + } + } }