PHPackages                             davidannebicque/html-to-spreadsheet-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. davidannebicque/html-to-spreadsheet-bundle

ActiveSymfony-bundle[PDF &amp; Document Generation](/categories/documents)

davidannebicque/html-to-spreadsheet-bundle
==========================================

A Symfony bundle to convert HTML tables to Excel spreadsheets using PhpSpreadsheet.

v0.2.0(3mo ago)7771[1 issues](https://github.com/Dannebicque/HtmlToSpreadsheetBundle/issues)MITPHP

Since Dec 2Pushed 3mo agoCompare

[ Source](https://github.com/Dannebicque/HtmlToSpreadsheetBundle)[ Packagist](https://packagist.org/packages/davidannebicque/html-to-spreadsheet-bundle)[ RSS](/packages/davidannebicque-html-to-spreadsheet-bundle/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (9)Versions (6)Used By (0)

HtmlToSpreadsheetBundle
=======================

[](#htmltospreadsheetbundle)

[![Latest Version](https://camo.githubusercontent.com/1a5ca3771c9b70175d68dca4c122e0e3106734bd1ea4880f84c734c19b42543b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461766964616e6e656269637175652f68746d6c2d746f2d73707265616473686565742d62756e646c652e737667)](https://packagist.org/packages/davidannebicque/html-to-spreadsheet-bundle)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)[![Symfony](https://camo.githubusercontent.com/f13da8cfd87be29839093448b9d154ac09ffa917c5c2fa82d47916b1b56c1b19/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53796d666f6e792d362e342f372e782f382e782d626c61636b2e737667)](https://symfony.com)[![PhpSpreadsheet](https://camo.githubusercontent.com/590582783160014e7ae6b4b4d04f62d81f553da0931ecbe1824dde9078b488c3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f50687053707265616473686565742d342e782f352e782d79656c6c6f772e737667)](https://github.com/PHPOffice/PhpSpreadsheet)

Warning
-------

[](#warning)

First draft of a Symfony bundle to convert HTML tables into Excel (.xlsx), OpenDocument (.ods) or CSV. WIP.

---

Convert **annotated HTML tables** into **Excel (.xlsx), OpenDocument (.ods)** or **CSV** using a simple, declarative syntax.

This bundle is ideal for developers who want to use **Twig + HTML** as a DSL to build spreadsheets, without directly using the PhpSpreadsheet API.

---

Requirements
------------

[](#requirements)

- PHP 8.2+
- Symfony 6.4+
- PhpSpreadsheet 4.x|5.x (maybe 3 also compatible, not tested)

Features
--------

[](#features)

- Write spreadsheets using **HTML tables** and `data-xls-*` attributes
- **One-liner rendering** with `SpreadsheetRenderer` service or `SpreadsheetTrait` for controllers
- Auto-generation of **Excel sheets**, **styles**, **formulas**, **freezepanes**, **column widths**, **images**, etc.
- Built-in **French-oriented presets** (`money`, `date`, `float2`, `percent2`, etc.)
- Generate **XLSX**, **ODS**, **CSV** with multi-format export support
- **Cell styling** (background colors, font sizes, borders, protection)
- **Image support** (local files, data-URI, remote HTTP/HTTPS URLs)
- Multiple sheets from multiple `` tags
- Strict mode (invalid attributes → exception)
- Fully extensible (register custom styles, validators, etc.)

---

Cell Styling Attributes
-----------------------

[](#cell-styling-attributes)

### Background &amp; Font

[](#background--font)

- `data-xls-bg-color="#FF0000"` - Set cell background color (hex format)
- `data-xls-font-size="14"` - Set font size (numeric value)

### Borders

[](#borders)

- `data-xls-border="thin"` - Set border style (`thin`, `medium`, `thick`, `none`)
- `data-xls-border-color="#000000"` - Set border color (hex format)

### Cell Protection

[](#cell-protection)

- `data-xls-locked="true"` - Lock cell (requires sheet protection)
- `data-xls-locked="false"` - Unlock cell for editing

### Example

[](#example)

```

    Protected Cell

```

Cell Merging
------------

[](#cell-merging)

Merge cells horizontally (colspan) or vertically (rowspan), or both simultaneously.

### Attributes

[](#attributes)

- `data-xls-colspan="N"` - Merge N columns horizontally
- `data-xls-rowspan="N"` - Merge N rows vertically

### Examples

[](#examples)

**Horizontal Merging (Colspan):**

```

    Merged Header

    A
    B
    C

```

**Vertical Merging (Rowspan):**

```

    Category
    Item 1

    Item 2

```

**Combined Merging:**

```

        Large Block

    C1

    C2

```

**Complex Table Example:**

```

            Q1 Results
            Q2 Results

            RevenueProfit
            RevenueProfit

            North
            100K20K
            120K25K

            110K22K
            130K28K

```

### Combining with Styling

[](#combining-with-styling)

Merged cells can be styled like any other cell:

```

    Styled Merged Cell

```

Font Styling
------------

[](#font-styling)

Control font appearance with these attributes:

### Font Color &amp; Weight

[](#font-color--weight)

- `data-xls-font-color="#FF0000"` - Set font color (hex format)
- `data-xls-font-bold="true"` - Make text bold (`true` or `false`)
- `data-xls-font-italic="true"` - Make text italic (`true` or `false`)

### Font Decoration &amp; Family

[](#font-decoration--family)

- `data-xls-font-underline="single"` - Underline text (`single`, `double`, or `none`)
- `data-xls-font-name="Arial"` - Set font family (e.g., "Arial", "Times New Roman", "Calibri")

### Example

[](#example-1)

```

    Styled Text

```

Conditional Formatting
----------------------

[](#conditional-formatting)

Apply dynamic formatting based on cell values using a simple syntax:

**Syntax:** `data-xls-conditional="condition|style1|style2..."`

### Available Conditions

[](#available-conditions)

- `value>X` - Greater than
- `value=X` - Greater than or equal
- `value
    -150.50

    1500.00

    250.00

```

Auto-size Columns
-----------------

[](#auto-size-columns)

Automatically adjust column widths to fit content. This feature uses PhpSpreadsheet's auto-sizing capability to calculate optimal column widths based on cell content.

### Attributes

[](#attributes-1)

**Table-level:**

- `data-xls-autosize="true"` - Auto-size all columns in the table
- `data-xls-autosize="A"` - Auto-size a single column (A)
- `data-xls-autosize="A:D"` - Auto-size a range of columns (A through D)
- `data-xls-autosize="A,C,E"` - Auto-size specific columns (A, C, and E)

**Column-level (in ``):**

- `data-xls-autosize="true"` - Auto-size this specific column

### Examples

[](#examples-2)

**Auto-size all columns:**

```

        Product A
        This is a much longer description
        $99.99

```

**Auto-size specific columns:**

```

        Name
        Fixed width column
        Long description text

```

**Auto-size columns by range:**

```

        ID
        Name
        Email
        Phone
        Notes

```

**Column-level auto-size with colgroup:**

```

        ID
        Variable width name
        Fixed
        Variable width description
## Cell Comments

Add comments (notes) to cells to provide additional information, instructions, or context. Comments appear as small indicators in cells and display when hovering over them in Excel/LibreOffice.

### Attributes

- `data-xls-comment="text"` - Comment text (required)
- `data-xls-comment-author="name"` - Comment author name (optional)
- `data-xls-comment-width="pixels"` - Comment box width in pixels (optional)
- `data-xls-comment-height="pixels"` - Comment box height in pixels (optional)
- `data-xls-comment-visible="true"` - Make comment always visible (optional, default: false)

### Examples

**Basic comment:**
```html

    Important data

```

**Comment with author:**

```

    1,234.56

```

**Comment with custom dimensions:**

```

    Complex data

```

**Always visible comment:**

```

    Critical value

```

**Complete example with all attributes:**

```

```

**Note:** Auto-size calculates width based on content after all data is loaded. If both `data-xls-width` and `data-xls-autosize` are specified, autosize takes precedence. **Use cases:**

- Provide instructions for data entry
- Add review notes or approvals
- Document formula logic
- Flag cells requiring attention
- Add contextual information for collaborators

Multi-Format Export
-------------------

[](#multi-format-export)

Export your spreadsheets in multiple formats:

```
// XLSX (default)
return $factory->streamWorkbook($workbook, 'export.xlsx');
// or explicitly
return $factory->streamWorkbook($workbook, 'export', 'xlsx');

// CSV (exports first sheet only)
return $factory->streamWorkbook($workbook, 'export', 'csv');

// ODS (OpenDocument - LibreOffice compatible)
return $factory->streamWorkbook($workbook, 'export', 'ods');
```

The file extension is automatically added if not present.

Image Support
-------------

[](#image-support)

Insert images in cells using various sources:

```

```

Hyperlinks
----------

[](#hyperlinks)

Add clickable links to cells with automatic styling. Links are rendered as blue, underlined text in Excel/ODS files.

### Attributes

[](#attributes-2)

- `data-xls-link="URL"` - Hyperlink URL (required)
- `data-xls-link-tooltip="text"` - Tooltip displayed on hover (optional)

### Supported Link Types

[](#supported-link-types)

**External URLs:**

```
Visit our website
GitHub Repository
```

**Internal References (to another sheet/cell):**

```

Go to Sheet2, cell A1

Jump to B10

Link to "My Data" sheet
```

**Email Addresses:**

```

Send email

Contact Support
```

**With Tooltips:**

```

    Symfony Docs

```

### Combining with Other Styles

[](#combining-with-other-styles)

Hyperlinks can be combined with other cell styling:

```

    Highlighted Link

```

### Automatic Styling

[](#automatic-styling)

Hyperlinks are automatically styled with:

- **Font color:** Blue (#0563C1)
- **Text decoration:** Underlined

These default styles can be overridden using `data-xls-font-color` and `data-xls-font-underline` attributes.

---

Installation
------------

[](#installation)

```
composer require davidannebicque/html-to-spreadsheet-bundle
```

Configuration
-------------

[](#configuration)

Create the file `config/packages/html_to_spreadsheet.yaml` :

```
html_to_spreadsheet:
    strict: true
    include_builtins: true
```

A complete example file with all available styles is provided in: `vendor/davidannebicque/html-to-spreadsheet-bundle/src/Resources/config/html_to_spreadsheet.yaml.dist`

### Styles prédéfinis disponibles

[](#styles-prédéfinis-disponibles)

- `th` : En-tête de tableau (gras, centré)
- `money` : Format monétaire Euro
- `date` : Format date courte
- `int` : Nombre entier
- `percent2` : Pourcentage à 2 décimales
- ... (voir la documentation complète)

### Créer des styles personnalisés

[](#créer-des-styles-personnalisés)

```
html_to_spreadsheet:
    default_styles:
        mon_style:
            font:
                bold: true
                color: 'FF0000'
            alignment:
                horizontal: 'center'
```

Quick Start
-----------

[](#quick-start)

### Simple Usage (Recommended)

[](#simple-usage-recommended)

Use the `SpreadsheetRenderer` service or `SpreadsheetTrait` for a one-liner solution:

```
