diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php
index 67b15d632d..a6f14792f8 100644
--- a/src/PhpWord/Writer/HTML/Element/Table.php
+++ b/src/PhpWord/Writer/HTML/Element/Table.php
@@ -51,6 +51,14 @@ public function write()
$rowCellCount = count($rowCells);
for ($j = 0; $j < $rowCellCount; $j++) {
$cellStyle = $rowCells[$j]->getStyle();
+ $cellBgColor = $cellStyle->getBgColor();
+ $cellFgColor = null;
+ if ($cellBgColor) {
+ $red = hexdec(substr($cellBgColor, 0, 2));
+ $green = hexdec(substr($cellBgColor, 2, 2));
+ $blue = hexdec(substr($cellBgColor, 4, 2));
+ $cellFgColor = (($red * 0.299 + $green * 0.587 + $blue * 0.114) > 186) ? null : 'ffffff';
+ }
$cellColSpan = $cellStyle->getGridSpan();
$cellRowSpan = 1;
$cellVMerge = $cellStyle->getVMerge();
@@ -74,7 +82,9 @@ public function write()
$cellTag = $tblHeader ? 'th' : 'td';
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : '');
$cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : '');
- $content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}>" . PHP_EOL;
+ $cellBgColorAttr = (is_null($cellBgColor) ? '' : " bgcolor=\"#{$cellBgColor}\"");
+ $cellFgColorAttr = (is_null($cellFgColor) ? '' : " color=\"#{$cellFgColor}\"");
+ $content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}{$cellBgColorAttr}{$cellFgColorAttr}>" . PHP_EOL;
$writer = new Container($this->parentWriter, $rowCells[$j]);
$content .= $writer->write();
if ($cellRowSpan > 1) {
diff --git a/tests/PhpWord/Writer/HTML/ElementTest.php b/tests/PhpWord/Writer/HTML/ElementTest.php
index 5b7176c71b..101e226f50 100644
--- a/tests/PhpWord/Writer/HTML/ElementTest.php
+++ b/tests/PhpWord/Writer/HTML/ElementTest.php
@@ -86,10 +86,10 @@ public function testWriteColSpan()
$section = $phpWord->addSection();
$table = $section->addTable();
$row1 = $table->addRow();
- $cell11 = $row1->addCell(1000, array('gridSpan' => 2));
+ $cell11 = $row1->addCell(1000, array('gridSpan' => 2, 'bgColor' => '6086B8'));
$cell11->addText('cell spanning 2 bellow');
$row2 = $table->addRow();
- $cell21 = $row2->addCell(500);
+ $cell21 = $row2->addCell(500, array('bgColor' => 'ffffff'));
$cell21->addText('first cell');
$cell22 = $row2->addCell(500);
$cell22->addText('second cell');
@@ -100,6 +100,11 @@ public function testWriteColSpan()
$this->assertEquals(1, $xpath->query('/html/body/table/tr[1]/td')->length);
$this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent);
$this->assertEquals(2, $xpath->query('/html/body/table/tr[2]/td')->length);
+
+ $this->assertEquals('#6086B8', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
+ $this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('color')->textContent);
+ $this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
+ $this->assertNull($xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('color'));
}
/**