Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
XLSX reader HTML writer does not support conditional formatting if using inline CSS
If you use the XLSX reader and HTML writer with the inline CSS option, conditional formatting color scale support is lost.

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Html;

$spreadsheet = IOFactory::load('example.xlsx');

$writer = new Html($spreadsheet);
$writer->setConditionalFormatting(true);
$writer->setUseInlineCss(true);

echo 'With `setUseInlineCss(true);`';

echo $writer->generateSheetData();

$writer->setUseInlineCss(false);

echo 'With `setUseInlineCss(false);`';

echo $writer->generateSheetData();
```

Steps to reproduce:

1. Download the `.zip` attached here
2. Run `php -S 0.0.0.0:8080`

[spreadsheet-test.zip](https://github.com/user-attachments/files/21244052/spreadsheet-test.zip)

Pull request:
Html Writer Conditional Formatting Inline Css
Fix #4539. Conditional Formatting was recently added to Html Writer. It works fine when not using inline Css. However, when using inline Css, the code inadvertently added 2 different `style` attributes (one for the unconditional style and one for the conditional style) to the same cell. This is not valid html, and results in losing the conditional styling. This PR combines the two `style` attributes into one, which will now come after the `class`, `colspan`, and `rowspan` attributes.

Aside from the new tests, this PR changes an unusually large number of existing tests. While this might normally be considered a red flag, it is not a problem here. All of the changes involve merely changing the order of attributes within html tags; none of them affect how the generated html would appear in a browser.

This is:

- [x] a bugfix
- [ ] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [x] Changes are covered by unit tests
  - [x] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d30c8034..a86794c05 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
 
 ### Fixed
 
+- Html Writer Conditional Formatting Inline Css. [Issue #4539](https://github.com/PHPOffice/PhpSpreadsheet/issues/4539) [PR #4541](https://github.com/PHPOffice/PhpSpreadsheet/pull/4541)
 - Do not use htmlspecialchars when formatting XML. [Issue #4537](https://github.com/PHPOffice/PhpSpreadsheet/issues/4537) [PR #4540](https://github.com/PHPOffice/PhpSpreadsheet/pull/4540)
 - Writer Html/Pdf support RTL alignment of tables. [Issue #1104](https://github.com/PHPOffice/PhpSpreadsheet/issues/1104) [PR #4535](https://github.com/PHPOffice/PhpSpreadsheet/pull/4535)
 - Xlsx Reader use dynamic arrays if spreadsheet did so. [PR #4533](https://github.com/PHPOffice/PhpSpreadsheet/pull/4533)
diff --git a/src/PhpSpreadsheet/Style/Conditional.php b/src/PhpSpreadsheet/Style/Conditional.php
index 736b72be5..2ef7cacd7 100644
--- a/src/PhpSpreadsheet/Style/Conditional.php
+++ b/src/PhpSpreadsheet/Style/Conditional.php
@@ -272,7 +272,7 @@ class Conditional implements IComparable
     public function getStyle(mixed $cellData = null): Style
     {
         if ($this->conditionType === self::CONDITION_COLORSCALE && $cellData !== null && $this->colorScale !== null && is_numeric($cellData)) {
-            $style = new Style();
+            $style = new Style(isConditional: true);
             $style->getFill()->setFillType(Fill::FILL_SOLID);
             $style->getFill()->getStartColor()->setARGB($this->colorScale->getColorForValue((float) $cellData));
 
diff --git a/src/PhpSpreadsheet/Style/ConditionalFormatting/StyleMerger.php b/src/PhpSpreadsheet/Style/ConditionalFormatting/StyleMerger.php
index 10f3ba65d..645f2387f 100644
--- a/src/PhpSpreadsheet/Style/ConditionalFormatting/StyleMerger.php
+++ b/src/PhpSpreadsheet/Style/ConditionalFormatting/StyleMerger.php
@@ -14,7 +14,10 @@ class StyleMerger
 
     public function __construct(Style $baseStyle)
     {
-        $this->baseStyle = $baseStyle;
+        // Setting to $baseStyle sometimes causes problems later on.
+        $array = $baseStyle->exportArray();
+        $this->baseStyle = new Style();
+        $this->baseStyle->applyFromArray($array);
     }
 
     public function getStyle(): Style
@@ -75,7 +78,11 @@ class StyleMerger
 
     protected function mergeBorderStyle(Border $baseBorderStyle, Border $borderStyle): void
     {
-        $baseBorderStyle->setBorderStyle($borderStyle->getBorderStyle());
+        if ($borderStyle->getBorderStyle() !== Border::BORDER_OMIT) {
+            $baseBorderStyle->setBorderStyle(
+                $borderStyle->getBorderStyle()
+            );
+        }
         if ($borderStyle->getColor()->getARGB() !== null) {
             $baseBorderStyle->setColor($borderStyle->getColor());
         }
diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php
index c79b5737a..d783ec945 100644
--- a/src/PhpSpreadsheet/Writer/Html.php
+++ b/src/PhpSpreadsheet/Writer/Html.php
@@ -1074,13 +1074,13 @@ class Html extends BaseWriter
      *
      * @return string[]
      */
-    private function createCSSStyle(Style $style): array
+    private function createCSSStyle(Style $style, bool $conditional = false): array
     {
         // Create CSS
         return array_merge(
-            $this->createCSSStyleAlignment($style->getAlignment()),
+            $conditional ? [] : $this->createCSSStyleAlignment($style->getAlignment()),
             $this->createCSSStyleBorders($style->getBorders()),
-            $this->createCSSStyleFont($style->getFont()),
+            $this->createCSSStyleFont($style->getFont(), conditional: $conditional),
             $this->createCSSStyleFill($style->getFill())
         );
     }
@@ -1124,7 +1124,7 @@ class Html extends BaseWriter
      *
      * @return string[]
      */
-    private function createCSSStyleFont(Font $font, bool $useDefaults = false): array
+    private function createCSSStyleFont(Font $font, bool $useDefaults = false, bool $conditional = false): array
     {
         // Construct CSS
         $css = [];
@@ -1151,12 +1151,29 @@ class Html extends BaseWriter
         }
 
         $css['color'] = '#' . $font->getColor()->getRGB();
-        $css['font-family'] = '\'' . htmlspecialchars((string) $font->getName(), ENT_QUOTES) . '\'';
-        $css['font-size'] = $font->getSize() . 'pt';
+        if (!$conditional) {
+            $css['font-family'] = '\'' . htmlspecialchars((string) $font->getName(), ENT_QUOTES) . '\'';
+            $css['font-size'] = $font->getSize() . 'pt';
+        }
 
         return $css;
     }
 
+    /**
+     * @param string[] $css
+     */
+    private function styleBorder(array &$css, string $index, Border $border): void
+    {
+        $borderStyle = $border->getBorderStyle();
+        // Mpdf doesn't process !important, so omit unimportant border none
+        if ($borderStyle === Border::BORDER_NONE && $this instanceof Pdf\Mpdf) {
+            return;
+        }
+        if ($borderStyle !== Border::BORDER_OMIT) {
+            $css[$index] = $this->createCSSStyleBorder($border);
+        }
+    }
+
     /**
      * Create CSS style.
      *
@@ -1170,26 +1187,10 @@ class Html extends BaseWriter
         $css = [];
 
         // Create CSS
-        if (!($this instanceof Pdf\Mpdf)) {
-            $css['border-bottom'] = $this->createCSSStyleBorder($borders->getBottom());
-            $css['border-top'] = $this->createCSSStyleBorder($borders->getTop());
-            $css['border-left'] = $this->createCSSStyleBorder($borders->getLeft());
-            $css['border-right'] = $this->createCSSStyleBorder($borders->getRight());
-        } else {
-            // Mpdf doesn't process !important, so omit unimportant border none
-            if ($borders->getBottom()->getBorderStyle() !== Border::BORDER_NONE) {
-                $css['border-bottom'] = $this->createCSSStyleBorder($borders->getBottom());
-            }
-            if ($borders->getTop()->getBorderStyle() !== Border::BORDER_NONE) {
-                $css['border-top'] = $this->createCSSStyleBorder($borders->getTop());
-            }
-            if ($borders->getLeft()->getBorderStyle() !== Border::BORDER_NONE) {
-                $css['border-left'] = $this->createCSSStyleBorder($borders->getLeft());
-            }
-            if ($borders->getRight()->getBorderStyle() !== Border::BORDER_NONE) {
-                $css['border-right'] = $this->createCSSStyleBorder($borders->getRight());
-            }
-        }
+        $this->styleBorder($css, 'border-bottom', $borders->getBottom());
+        $this->styleBorder($css, 'border-top', $borders->getTop());
+        $this->styleBorder($css, 'border-left', $borders->getLeft());
+        $this->styleBorder($css, 'border-right', $borders->getRight());
 
         return $css;
     }
@@ -1393,7 +1394,11 @@ class Html extends BaseWriter
             $style = isset($this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row])
                 ? $this->assembleCSS($this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row]) : '';
 
-            $html .= '          <tr style="' . $style . '">' . PHP_EOL;
+            if ($style === '') {
+                $html .= '          <tr>' . PHP_EOL;
+            } else {
+                $html .= '          <tr style="' . $style . '">' . PHP_EOL;
+            }
         }
 
         return $html;
@@ -1605,6 +1610,7 @@ class Html extends BaseWriter
                 $html .= ' data-type="' . DataType::TYPE_STRING . '"';
             }
         }
+        $holdCss = '';
         if (!$this->useInlineCss && !$this->isPdf && is_string($cssClass)) {
             $html .= ' class="' . $cssClass . '"';
             if ($htmlx) {
@@ -1650,9 +1656,17 @@ class Html extends BaseWriter
                 $xcssClass['position'] = 'relative';
             }
             /** @var string[] $xcssClass */
-            $html .= ' style="' . $this->assembleCSS($xcssClass) . '"';
+            $holdCss = $this->assembleCSS($xcssClass);
             if ($this->useInlineCss) {
-                $html .= ' class="gridlines gridlinesp"';
+                $prntgrid = $worksheet->getPrintGridlines();
+                $viewgrid = $this->isPdf ? $prntgrid : $worksheet->getShowGridlines();
+                if ($viewgrid && $prntgrid) {
+                    $html .= ' class="gridlines gridlinesp"';
+                } elseif ($viewgrid) {
+                    $html .= ' class="gridlines"';
+                } elseif ($prntgrid) {
+                    $html .= ' class="gridlinesp"';
+                }
             }
         }
 
@@ -1700,14 +1714,23 @@ class Html extends BaseWriter
                 }
             }
             if ($matched) {
-                $styles = $this->createCSSStyle($styleMerger->getStyle());
+                $styles = $this->createCSSStyle($styleMerger->getStyle(), true);
                 $html .= ' style="';
+                if ($holdCss !== '') {
+                    $html .= "$holdCss; ";
+                    $holdCss = '';
+                }
                 foreach ($styles as $key => $value) {
-                    $html .= $key . ':' . $value . ';';
+                    if (!str_starts_with($key, 'border-') || $value !== 'none #000000') {
+                        $html .= $key . ':' . $value . ';';
+                    }
                 }
                 $html .= '"';
             }
         }
+        if ($holdCss !== '') {
+            $html .= ' style="' . $holdCss . '"';
+        }
 
         $html .= '>';
         $html .= $htmlx;


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Writer/Dompdf/HideMergeTest.php b/tests/PhpSpreadsheetTests/Writer/Dompdf/HideMergeTest.php
index 7bbe5c6cc..a8b365baa 100644
--- a/tests/PhpSpreadsheetTests/Writer/Dompdf/HideMergeTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Dompdf/HideMergeTest.php
@@ -99,7 +99,7 @@ class HideMergeTest extends TestCase
         self::assertStringContainsString(
             '<tr class="row1">'
                 . '<td class="column0 style0" style="width:42pt; height:17pt">&nbsp;</td>'
-                . '<td class="column1 style1 s style1" style="width:84pt; height:17pt" colspan="2" rowspan="2">Hello World Headline</td>'
+                . '<td class="column1 style1 s style1" colspan="2" rowspan="2" style="width:84pt; height:17pt">Hello World Headline</td>'
                 . '</tr>',
             $html
         );
@@ -112,8 +112,8 @@ class HideMergeTest extends TestCase
         self::assertStringContainsString(
             '<tr class="row3">'
                 . '<td class="column0 style0" style="width:42pt; height:17pt">&nbsp;</td>'
-                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 1</td>'
-                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 1</td>'
+                . '<td class="column1 style2 s style2" rowspan="2" style="width:42pt; height:17pt">Label 1</td>'
+                . '<td class="column2 style3 s style3" rowspan="2" style="width:42pt; height:17pt">Text 1</td>'
                 . '</tr>',
             $html
         );
@@ -126,8 +126,8 @@ class HideMergeTest extends TestCase
         self::assertStringContainsString(
             '<tr class="row5">'
                 . '<td class="column0 style0" style="width:42pt; height:17pt">&nbsp;</td>'
-                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 2</td>'
-                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 2</td>'
+                . '<td class="column1 style2 s style2" rowspan="2" style="width:42pt; height:17pt">Label 2</td>'
+                . '<td class="column2 style3 s style3" rowspan="2" style="width:42pt; height:17pt">Text 2</td>'
                 . '</tr>',
             $html
         );
diff --git a/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php b/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php
index 71d81122a..e033c8ab4 100644
--- a/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php
@@ -139,6 +139,7 @@ class BetterBooleanTest extends Functional\AbstractFunctional
     {
         $spreadsheet = new Spreadsheet();
         $sheet = $spreadsheet->getActiveSheet();
+        $sheet->setPrintGridlines(true);
         $sheet->getCell('A1')->setValue(1);
         $sheet->getCell('B1')->setValue('Hello');
         $sheet->getCell('C1')->setValue(true);
@@ -153,15 +154,15 @@ class BetterBooleanTest extends Functional\AbstractFunctional
         $writer->setUseInlineCss(true);
         $html = $writer->generateHtmlAll();
         $html = str_replace('vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; ', '', $html);
-        $html = str_replace(' width:42pt" class="gridlines gridlinesp"', '"', $html);
+        $html = str_replace(' width:42pt"', '"', $html);
         self::assertStringNotContainsString('TRUE', $html);
-        self::assertStringContainsString('<td style="text-align:right;">1</td>', $html);
-        self::assertStringContainsString('<td style="text-align:left;">Hello</td>', $html);
-        self::assertStringContainsString('<td data-type="b" style="text-align:center;">VRAI</td>', $html);
-        self::assertStringContainsString('<td data-type="b" style="text-align:center;">FAUX</td>', $html);
-        self::assertStringContainsString('<td data-type="s" style="text-align:left;">1</td>', $html);
-        self::assertStringContainsString('<td style="text-align:left;">AB</td>', $html);
-        self::assertStringContainsString('<td style="text-align:right;">3</td>', $html);
+        self::assertStringContainsString('<td class="gridlines gridlinesp" style="text-align:right;">1</td>', $html);
+        self::assertStringContainsString('<td class="gridlines gridlinesp" style="text-align:left;">Hello</td>', $html);
+        self::assertStringContainsString('<td data-type="b" class="gridlines gridlinesp" style="text-align:center;">VRAI</td>', $html);
+        self::assertStringContainsString('<td data-type="b" class="gridlines gridlinesp" style="text-align:center;">FAUX</td>', $html);
+        self::assertStringContainsString('<td data-type="s" class="gridlines gridlinesp" style="text-align:left;">1</td>', $html);
+        self::assertStringContainsString('<td class="gridlines gridlinesp" style="text-align:left;">AB</td>', $html);
+        self::assertStringContainsString('<td class="gridlines gridlinesp" style="text-align:right;">3</td>', $html);
 
         $reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $this->setBetter(...));
         $spreadsheet->disconnectWorksheets();
diff --git a/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php b/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
index e4f292c2d..617743463 100644
--- a/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
+++ b/tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
@@ -15,6 +15,8 @@ class Issue3678Test extends TestCase
     {
         $spreadsheet = new Spreadsheet();
         $sheet = $spreadsheet->getActiveSheet();
+        $sheet->setShowGridlines(false);
+        $sheet->setPrintGridlines(true);
         $sheet->getCell('A1')->setValue(1);
         $styleArray = [
             'fill' => [
@@ -34,7 +36,10 @@ class Issue3678Test extends TestCase
         self::assertStringContainsString('.n { text-align:right }', $html);
         $writer->setUseInlineCss(true);
         $html = $writer->generateHtmlAll();
-        self::assertStringContainsString('<td style="' . $style2 . '" class="gridlines gridlinesp">1</td>', $html);
+        self::assertStringContainsString('<td class="gridlinesp" style="' . $style2 . '">1</td>', $html);
+        $sheet->setPrintGridlines(false);
+        $html = $writer->generateHtmlAll();
+        self::assertStringContainsString('<td style="' . $style2 . '">1</td>', $html);
         $spreadsheet->disconnectWorksheets();
     }
 }
diff --git a/tests/PhpSpreadsheetTests/Writer/Html/Issue4539Test.php b/tests/PhpSpreadsheetTests/Writer/Html/Issue4539Test.php
new file mode 100644
index 000000000..ee9924c7a
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Writer/Html/Issue4539Test.php
@@ -0,0 +1,48 @@
+<?php
+
+declare(strict_types=1);
+
+namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
+
+use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
+use PhpOffice\PhpSpreadsheet\Writer\Html;
+use PHPUnit\Framework\TestCase;
+
+class Issue4539Test extends TestCase
+{
+    public function testInlineAndNot(): void
+    {
+        $infile = 'tests/data/Reader/XLSX/issue.4539.xlsx';
+        $reader = new XlsxReader();
+        $spreadsheet = $reader->load($infile);
+        $writer = new Html($spreadsheet);
+        $writer->setConditionalFormatting(true);
+        $writer->setUseInlineCss(true);
+        $html = $writer->generateHtmlAll();
+        $expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; color:#000000;background-color:#5A8AC6;">5</td>';
+        self::assertStringContainsString($expected, $html, 'inline conditional style');
+        $expected = '<td style="vertical-align:bottom; font-weight:bold; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:left; width:102pt">Column Heading</td>';
+        self::assertStringContainsString($expected, $html, 'inline no conditional style');
+        $expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; border-top:1px solid #92D050 !important;color:#000000;">1</td>';
+        self::assertStringContainsString($expected, $html, 'inline border-top');
+        $expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt; border-top:1px solid #FF0000 !important;font-weight:bold;color:#000000;">2</td>';
+        self::assertStringContainsString($expected, $html, 'inline border-top and bold');
+        $expected = '<td style="vertical-align:bottom; color:#000000; font-family:\'Aptos Narrow\'; font-size:12pt; text-align:right; width:102pt">3</td>';
+        self::assertStringContainsString($expected, $html, 'inline nomatch');
+
+        $writer->setUseInlineCss(false);
+        $html = $writer->generateHtmlAll();
+        $expected = '<td class="column0 style0 n" style="color:#000000;background-color:#5A8AC6;">5</td>';
+        self::assertStringContainsString($expected, $html, 'notinline conditional style');
+        $expected = '<td class="column0 style1 s">Column Heading</td>';
+        self::assertStringContainsString($expected, $html, 'notinline no conditional style');
+        $expected = '<td class="column0 style0 n" style="border-top:1px solid #92D050 !important;color:#000000;">1</td>';
+        self::assertStringContainsString($expected, $html, 'notinline border-top');
+        $expected = '<td class="column0 style0 n" style="border-top:1px solid #FF0000 !important;font-weight:bold;color:#000000;">2</td>';
+        self::assertStringContainsString($expected, $html, 'notinline border-top bold');
+        $expected = '<td class="column0 style0 n">3</td>';
+        self::assertStringContainsString($expected, $html, 'notinline nomatch');
+
+        $spreadsheet->disconnectWorksheets();
+    }
+}
diff --git a/tests/PhpSpreadsheetTests/Writer/Html/NoTitleTest.php b/tests/PhpSpreadsheetTests/Writer/Html/NoTitleTest.php
index 7faf8a6ad..4a6a5f4c2 100644
--- a/tests/PhpSpreadsheetTests/Writer/Html/NoTitleTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Html/NoTitleTest.php
@@ -23,7 +23,7 @@ class NoTitleTest extends TestCase
         $writer->setUseInlineCss(true);
         $html = $writer->generateHTMLAll();
         self::assertStringContainsString('<title>Sheet1</title>', $html);
-        self::assertStringContainsString('<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt" class="gridlines gridlinesp">C1</td>', $html);
+        self::assertStringContainsString('<td class="gridlines" style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt">C1</td>', $html);
         $writer->setUseInlineCss(false);
         $html = $writer->generateHTMLAll();
         self::assertStringContainsString('<td class="column2 style0 s">C1</td>', $html);
@@ -55,8 +55,8 @@ class NoTitleTest extends TestCase
         $writer = new Html($spreadsheet);
         $writer->setUseInlineCss(true);
         $html = $writer->generateHTMLAll();
-        self::assertStringContainsString('<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt" class="gridlines gridlinesp">7</td>', $html);
-        self::assertStringContainsString('<td style="vertical-align:bottom; border-bottom:none #808080; border-top:none #808080; border-left:none #808080; border-right:none #808080; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt" class="gridlines gridlinesp">19</td>', $html);
+        self::assertStringContainsString('<td class="gridlines" style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">7</td>', $html);
+        self::assertStringContainsString('<td class="gridlines" style="vertical-align:bottom; border-bottom:none #808080; border-top:none #808080; border-left:none #808080; border-right:none #808080; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">19</td>', $html);
         $spreadsheet->disconnectWorksheets();
     }
 }
diff --git a/tests/PhpSpreadsheetTests/Writer/Mpdf/HideMergeTest.php b/tests/PhpSpreadsheetTests/Writer/Mpdf/HideMergeTest.php
index d6f9b0270..9753bed44 100644
--- a/tests/PhpSpreadsheetTests/Writer/Mpdf/HideMergeTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Mpdf/HideMergeTest.php
@@ -93,7 +93,7 @@ class HideMergeTest extends TestCase
         );
         self::assertStringContainsString(
             '<tr class="row1">'
-                . '<td class="column1 style1 s style1" style="width:84pt; height:17pt" colspan="2" rowspan="2">Hello World Headline</td>'
+                . '<td class="column1 style1 s style1" colspan="2" rowspan="2" style="width:84pt; height:17pt">Hello World Headline</td>'
                 . '</tr>',
             $html
         );
@@ -104,8 +104,8 @@ class HideMergeTest extends TestCase
         );
         self::assertStringContainsString(
             '<tr class="row3">'
-                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 1</td>'
-                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 1</td>'
+                . '<td class="column1 style2 s style2" rowspan="2" style="width:42pt; height:17pt">Label 1</td>'
+                . '<td class="column2 style3 s style3" rowspan="2" style="width:42pt; height:17pt">Text 1</td>'
                 . '</tr>',
             $html
         );
@@ -116,8 +116,8 @@ class HideMergeTest extends TestCase
         );
         self::assertStringContainsString(
             '<tr class="row5">'
-                . '<td class="column1 style2 s style2" style="width:42pt; height:17pt" rowspan="2">Label 2</td>'
-                . '<td class="column2 style3 s style3" style="width:42pt; height:17pt" rowspan="2">Text 2</td>'
+                . '<td class="column1 style2 s style2" rowspan="2" style="width:42pt; height:17pt">Label 2</td>'
+                . '<td class="column2 style3 s style3" rowspan="2" style="width:42pt; height:17pt">Text 2</td>'
                 . '</tr>',
             $html
         );
diff --git a/tests/PhpSpreadsheetTests/Writer/Tcpdf/HideMergeTest.php b/tests/PhpSpreadsheetTests/Writer/Tcpdf/HideMergeTest.php
index 04284c081..8eea9a1b4 100644
--- a/tests/PhpSpreadsheetTests/Writer/Tcpdf/HideMergeTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Tcpdf/HideMergeTest.php
@@ -17,6 +17,7 @@ class HideMergeTest extends TestCase
     {
         $spreadsheet = new Spreadsheet();
         $worksheet = $spreadsheet->getActiveSheet();
+        $worksheet->setPrintGridlines(true);
         // just some labels for better visualisation of the problem
         $worksheet->setCellValue('A1', 'A');
         $worksheet->setCellValue('B1', 'B');
@@ -87,15 +88,15 @@ class HideMergeTest extends TestCase
         self::assertStringContainsString(
             '<tbody><tr style="height:17pt">'
                 . '<td></td>'
-                . '<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp">B</td>'
-                . '<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp">C</td>'
+                . '<td class="gridlines gridlinesp" style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">B</td>'
+                . '<td class="gridlines gridlinesp" style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">C</td>'
                 . '</tr>',
             $html
         );
         self::assertStringContainsString(
             '<tr style="height:17pt">'
                 . '<td></td>'
-                . '<td style="vertical-align:bottom; text-align:center; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; width:84pt; height:17pt" class="gridlines gridlinesp" colspan="2" rowspan="2">Hello World Headline</td>'
+                . '<td class="gridlines gridlinesp" colspan="2" rowspan="2" style="vertical-align:bottom; text-align:center; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; width:84pt; height:17pt">Hello World Headline</td>'
                 . '</tr>',
             $html
         );
@@ -109,16 +110,16 @@ class HideMergeTest extends TestCase
         self::assertStringContainsString(
             '<tr style="height:17pt">'
                 . '<td></td>'
-                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Label 1</td>'
-                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Text 1</td>'
+                . '<td class="gridlines gridlinesp" rowspan="2" style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">Label 1</td>'
+                . '<td class="gridlines gridlinesp" rowspan="2" style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">Text 1</td>'
                 . '</tr>',
             $html
         );
         self::assertStringContainsString(
             '<tr style="height:17pt">'
                 . '<td></td>'
-                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Label 2</td>'
-                . '<td style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt" class="gridlines gridlinesp" rowspan="2">Text 2</td>'
+                . '<td class="gridlines gridlinesp" rowspan="2" style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; font-weight:bold; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">Label 2</td>'
+                . '<td class="gridlines gridlinesp" rowspan="2" style="vertical-align:middle; border-bottom:1px solid #000000 !important; border-top:1px solid #000000 !important; border-left:1px solid #000000 !important; border-right:1px solid #000000 !important; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt; height:17pt">Text 2</td>'
                 . '</tr>',
             $html
         );
diff --git a/tests/PhpSpreadsheetTests/Writer/Tcpdf/MergedBorderTest.php b/tests/PhpSpreadsheetTests/Writer/Tcpdf/MergedBorderTest.php
index 5b61c649a..9c3df41f8 100644
--- a/tests/PhpSpreadsheetTests/Writer/Tcpdf/MergedBorderTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Tcpdf/MergedBorderTest.php
@@ -32,7 +32,7 @@ class MergedBorderTest extends TestCase
         $sheet->setShowGridlines(false);
         $writer = new Tcpdf($spreadsheet);
         $html = $writer->generateHtmlAll();
-        self::assertSame(1, preg_match('/border-bottom:1px solid #FF0000 !important; border-top:1px solid #FF0000 !important; border-left:1px solid #FF0000 !important; border-right:1px solid #FF0000 !important; color:#000000;[^>]+ colspan="2" rowspan="4"/', $html));
+        self::assertSame(1, preg_match('/ colspan="2" rowspan="4" style="vertical-align:bottom; border-bottom:1px solid #FF0000 !important; border-top:1px solid #FF0000 !important; border-left:1px solid #FF0000 !important; border-right:1px solid #FF0000 !important; color:#000000;[^>]+/', $html));
         $spreadsheet->disconnectWorksheets();
     }
 }