Forum


Replies: 1   Views: 404
Add html to one cell only
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpxlsx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpxlsx version and check the Documentation available.

Posted by jeroen8087  · 09-02-2023 - 09:32

When using addHTML, every block element in the HTML will be put in the next row. It would be nice to have an option to put all HTML in one cell only.

For example, when creating an Excel export of some data set, every row in the Excel sheet represents one row in the data set. If a field in the data set contains HTML, we want to have all HTML in one cell in the Excel output.

Please see this example:

$data = [
    [
        "col1 in row 1",
        "<p>Row 1 sentence</p><p>Another row 1 sentence.</p>"
    ],
    [
        "col1 in row 2",
        "<p>Row 2 sentence</p><p>Another row 2 sentence.</p>"
    ]
];

$xlsx = new CreateXlsx();

// row 1
$xlsx->addCell($data[0][0], 'A1');
$xlsx->addHtml($data[0][1], 'B1');
// row 2
$xlsx->addCell($data[1][0], 'A2');
$xlsx->addHtml($data[1][1], 'B2');

Problems:

  • Cell B1 will contain "Row 1 sentence".
  • Cell B2 will contain "Row 2 sentence"
  • Cell B3 will contain "Another row 2 sentence."

In this case, it is preferred to replace the paragraph break in the HTML with a new line, so the complete contents of the HTML will appear in one cell.

This limits lay-out options (e.g. <h1> cannot be rendered as a Heading 1 cell), but that does not have to be a problem.