Forum


Replies: 3   Views: 452
Convert html table to excel
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:39

It would be nice to convert a HTML table to Excel.

In this example I use the non-existing function addHtmlTable:

$table = <<<HTML
<table>
    <tr>
        <td>Some content here</td>
        <td>More content here</td>
    </tr>
    <tr>
        <td>Some content for the next row</td>
        <td>More content!</td>
    </tr>
</table>    
HTML;

$xlsx = new CreateXlsx();

// This will add the table to cell A1. The table has two rows and two columns, so the table will be put in cell range A1:B2.
$xlsx->addHtmlTable($table, 'A1');

// This will add the table to cell C5. The table has two rows and two columns, so the table will be put in cell range C5:D6.
$xlsx->addHtmlTable($table, 'C5');

The contents of each <td> can be added by using addHtml internally, while putting all nodes in the td in one cell. See my other forum topic.

Posted by admin  · 09-02-2023 - 10:02

Hello,

The next stable release of phpxlsx will include support to transform HTML tables:

- HTML to XLSX supports transforming tables.

For example:

$html = '
<style>
    .cstable2 {
        data-style: TableStyleMedium2;
    }
    tr.rowstyles {
        font-family: Arial;
        font-weight: bold;
        text-decoration: underline;
        color: #2CA87A;
    }
</style>
<table class="cstable2">
    <tbody>
        <tr>
            <td style="background-color: yellow;">Cell 1 1</td>
            <td>Cell 1 2</td>
        </tr>
        <tr>
            <td>Cell 2 1</td>
            <td></td>
        </tr>
        <tr class="rowstyles">
            <td><em>Cell</em> 3 1</td>
            <td><em>Cell</em> 3 2</td>
        </tr>
    </tbody>
</table>
<p>Paragraph after table.</p>
';
$xlsx->addHtml($html, 'B3');

// table with th tags, that are added as table headers
$html = '
<style>
    tr.rowstyles {
        font-family: Arial;
        font-weight: bold;
        text-decoration: underline;
        color: #2CA87A;
    }
</style>
<table>
    <tbody>
        <tr>
            <th>Col A</th>
            <th>Col B</th>
        </tr>
        <tr>
            <td>Cell 2 1</td>
            <td>Cell 2 2</td>
        </tr>
        <tr class="rowstyles">
            <td><em>Cell</em> 3 1</td>
            <td><em>Cell</em> 3 2</td>
        </tr>
    </tbody>
</table>
';
$xlsx->addHtml($html, 'G3');

Regards.

Posted by jeroen8087  · 09-02-2023 - 10:39

That is great news! Thank you for your quick response.

When can I expect the next version? Is there a public roadmap and/or release schedule available?

Posted by admin  · 09-02-2023 - 10:52

Hello,

There's no release date for phpxlsx 2.5. The next week phppptx (https://www.phppptx.com/) will be officially announced. After phppptx has been launched the dev team will choose a date to release phpxlsx 2.5 and we'll send a newsletter.

phpxlsx 2.5 will include the following features:

=== What's new on phpxlsx 2.5? ===

- New methods to insert comments: addComment and addCommentAuthor.
- New method to insert defined names: addDefinedName.
- New method to add a macro: addMacroFromXlsx.
- Indexer (Advanced and Premium licenses):
  - Comments.
  - Defined names.
  - Cell positions.
  - Cell formats.
- Enabled fullCalcOnLoad option in the default base template.
- HTML to XLSX supports transforming tables.
- Improvements working with tables:
  - Add title and description properties.
  - Apply content types to contents and totals.
  - addTable returns the position range used.
- New options in setWorkbookSettings: readOnly, fullCalcOnLoad, forceFullCalc.
- New options in addSlide: position, color.
- New setCellValue method to set a cell value keeping existing styles.
- The getCell method returns the style index value.
- Images can be added from files, streams and base64 contents.
- Applied htmlspecialchars to content values when using template methods.
- Improved handle XLSX templates that use uppercase ContentType extensions.
- Added '/' as delimiter in preg_quote when extracting the template variables.
- The static variables $_templateSymbolStart and $_templateSymbolEnd available in the CreateXlsxFromTemplate class have been changed to class attributes.
- Free DOMDocument resources when using Indexer (Advanced and Premium licenses).
- The overwrite option has been renamed to replace in all methods.
- Sign class avoid generating partial signed files in the latest revisions of MS Office (Premium licenses).

Regards.