HTML to Excel with PHP

HTML to Excel

Introduction

The conversion of HTML into Excel is one of the most requested functionalities of phpxlsx.

phpxlsx supports creating Excel spreadsheets using HTML tags and CSS styles with the addHtml method.

To get the best XLSX output this feature provides, please install and enable PHP Tidy and PHP mbstring extensions on your server.

Examples

The code needed to insert some HTML with styles is as simple as this:

That generates the following output:

 

Another example that inserts tables:

That generates the following output:

 

Supported HTML tags, attributes and styles
  • a tags. A link is added as block content and fills a cell position due to XLSX requirements

    • see text element styles
  • p, h1, h2, h3, h4, h5, h6 tags

    • background-color
    • border: color, width
    • dir: ltr, rtl
    • text-align: left, center, right
    • vertical-align: top, center, bottom
  • span, b, cite, em, i, s, strong, sub, sup, var tags

    • see text element styles
  • table, tr, td, th tags

    • background-color
    • dir: ltr, rtl
    • text-align: left, center, right
    • vertical-align: top, center, bottom

    th tags are added as column names.

  • text element styles

    • color
    • font-family
    • font-size
    • font-style: italic, oblique
    • font-weight: bold, bolder, 700, 800, 900
    • text-decoration: line-through, underline
    • vertical-align: sub, sup
CSS variables

phpxlsx supports CSS variables by enabling the parseCSSVars option.

The following sample transforms HTML contents with CSS variables:

 

HTML Extended and CSS Extended

Premium licenses include HTML Extended and CSS Extended modes to invoke phpxlsx methods with custom HTML tags and apply custom styles not supported by standard HTML tags and CSS styles.

Thus, it is possible to insert sheets, breaks, and other contents and styles. All of it integrated with the supported HTML tags and CSS styles.

The following sample illustrates the use of HTML Extended and CSS Extended: