Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 – FastExcelWriter #33

Merged
merged 17 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
php: ['7.3', '7.4', '8.0', '8.1', '8.2']
php: ['8.0', '8.1', '8.2']
stability: ['prefer-lowest', 'prefer-stable']

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand All @@ -37,4 +37,7 @@ jobs:
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --verbose
run: vendor/bin/phpunit --verbose

- name: Check coding standard
run: vendor/bin/php-cs-fixer --no-interaction --dry-run --diff -v fix lib/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/example/test.xlsx
/example/~$test.xlsx
/.phpunit.result.cache
/.php-cs-fixer.cache
72 changes: 35 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://github.com/Ticketpark/HtmlPhpExcel/actions/workflows/tests.yml/badge.svg)](https://github.com/Ticketpark/HtmlPhpExcel/actions)

This is a php library based on [PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) which simplifies converting html tables to excel files. It allows styling right within the html template with specific attributes.
This is a php library based on [FastExcelWriter](https://github.com/aVadim483/fast-excel-writer), simplifying converting html tables to excel files. It allows styling right within the html template with specific attributes.

## Installation

Expand All @@ -16,68 +16,66 @@ composer require ticketpark/htmlphpexcel
```php
<?php

require_once('../vendor/autoload.php');
require_once('vendor/autoload.php');

$html = '<table><tr><th>Column A</th><th>Column B</th></tr><tr><td>Value A</td><td>Value B</td></tr></table>';
$htmlPhpExcel = new \Ticketpark\HtmlPhpExcel\HtmlPhpExcel($html);

// Create and output the excel file to the browser
$htmlPhpExcel->process()->output();

// Alternatively create the excel and save to a file
$htmlPhpExcel->process()->save('myFile.xlsx');

// or get the \PhpOffice\PhpSpreadsheet\Spreadsheet object to do further work with it
$phpExcelObject = $htmlPhpExcel->process()->getExcelObject();

```

For a more complex example with styling options see [example directory](example).
For a more complex example see [example directory](example).

## Styling
There is support for specific html attributes to allow styling of the excel output. The attributes expect the content to be json_encoded.

* `_excel-styles`<br>Supports everything which is possible with PhpSpreadsheet's `applyFromArray()` method ([also see here](https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#valid-array-keys-for-style-applyfromarray)).
Styles are set with an html element `_excel-styles`. The attribute expects the content to be in json format.

Example:
```html
<table>
<tr>
<td _excel-styles='{"font":{"size":16,"color":{"rgb":"FF0000"}}}'>Foo</td>
<tr _excel-styles='{"height": 50}'>
<td _excel-styles='{"font-size": 16, "font-color": "#FF0000", "width": 200}'>
Cell value
</td>
</tr>
</table>
```

* `_excel-dimensions`<br>Supports changing dimensions of rows (when applied to a `<tr>` or `<td>`) or column (when applied to a `<td>`),
You can use any style supported by `fast-excel-writer`, of which the most common are:

Example:
```html
<table>
<tr _excel-dimensions='{"row":{"rowHeight":50}}'>
<td _excel-dimensions='{"column":{"width":20}}'>Foo</td>
</tr>
</table>
```
* border-color
* border-style
* fill-color
* fill-pattern
* font-color
* font-size
* format
* format-text-wrap
* height
* text-align
* text-color
* text-rotation
* text-wrap
* vertical-align
* width

More information (though unfortunately limited) is available in the [docs of FastExcelWriter](https://github.com/aVadim483/fast-excel-writer/blob/master/docs/04-styles.md).

* `_excel-explicit`<br>Supports applying an explicit cell value type.
## Adding comments to cells

Example:
```html
<table>
<tr>
<td _excel-explicit='PhpSpreadsheet_Cell_DataType::TYPE_STRING'>0022</td>
<tr >
<td _excel-comment="This is a comment.">
Cell value
</td>
</tr>
</table>
```

* `_excel-comment`<br>Adds comment to cell.

Examle:
```html
<table>
<tr>
<td _excel-comment='Comment content'>Cell value</td>
</tr>
</table>
```
## History

* v2.x of this library is based on `FastExcelWriter`
* v1.x of this library was based on `PhpSpreadsheet`
* v0.x of this library was based on `PhpExcel`
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "ticketpark/htmlphpexcel",
"type": "library",
"description": "A php library based on PHPExcel to convert html tables to Excel files, including styling.",
"keywords": ["html", "tables", "excel", "phpexcel"],
"description": "A php library based to convert html tables to Excel files, including styling.",
"keywords": ["html", "tables", "excel", "phpexcel", "phpspreadsheet", "fastexcelwriter"],
"homepage": "https://github.com/Ticketpark/HtmlPhpExcel",
"license": "MIT",
"authors": [
{"name": "Manuel Reinhard", "email": "[email protected]"}
],
"require": {
"php": "^7.3|^8.0.0|^8.1.0|^8.2.0",
"php": "^8.0.0|^8.1.0|^8.2.0",
"ext-json": "*",
"ext-dom": "*",
"doctrine/collections": "^1.6|^2.0",
"phpoffice/phpspreadsheet": "^1.23"
"ext-intl": "*",
"avadim/fast-excel-writer": "^4.5"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.0",
"rector/rector": "^0.18.5",
"friendsofphp/php-cs-fixer": "^3.35"
},
"autoload": {
"psr-4": { "Ticketpark\\HtmlPhpExcel\\": "lib/HtmlPhpExcel" }
Expand Down
29 changes: 13 additions & 16 deletions example/example.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<table>

<tr>
<!-- Apply json-encoded styles to cells -->
<th _excel-styles='{"font":{"size":16,"color":{"rgb":"FF0000"}}}'>An Excel worksheet</th>
<th _excel-styles='{"font-size":16, "font-color":"#FF0000", "font-style":"bold"}'>An Excel worksheet</th>
</tr>

<tr>
<!-- Empty row -->
</tr>

<!-- Apply json-encoded styles to rows -->
<tr _excel-styles='{"fill":{"fillType":"PhpSpreadsheet_Style_Fill::FILL_SOLID","color":{"argb":"4F4F4F"}}}'>
<th _excel-styles='{"font":{"color":{"rgb":"FFFFFF"}}}'>Column A</th>
<th _excel-styles='{"font":{"color":{"rgb":"FFFFFF"}}}'>Column B</th>
<tr _excel-styles='{"fill-color":"#4F4F4F"}'>
<th _excel-styles='{"font-color":"#FFFFFF"}'>Column A</th>
<th _excel-styles='{"font-color":"#FFFFFF"}'>Column B</th>
</tr>

<tr>
<td>Left align</td>
<td _excel-styles='{"alignment":{"horizontal":"PhpSpreadsheet_Style_Alignment::HORIZONTAL_RIGHT"}}'>Right align</td>
<td _excel-styles='{"text-align": "right"}'>Right align</td>
</tr>

<tr>
<td>5</td>
<td>6</td>
<!-- Set explicit cell value types, e.q. to treat numbers as strings -->
<td _excel-explicit='PhpSpreadsheet_Cell_DataType::TYPE_STRING'>0022</td>
<td _excel-styles='{"format": "@text"}'>0022</td>
</tr>

<tr>
Expand All @@ -35,23 +36,19 @@
</tr>

<!-- Set row dimensions-->
<tr _excel-dimensions='{"row":{"rowHeight":50}}' _excel-styles='{"alignment":{"vertical":"PhpSpreadsheet_Style_Alignment::VERTICAL_CENTER"}}'>
<tr _excel-styles='{"height": 50, "text-align": "center"}'>
<!-- Number formats! -->
<td _excel-styles='{"numberFormat":{"formatCode":"PhpSpreadsheet_Style_NumberFormat::FORMAT_NUMBER_00"}}'>25</td>
<td _excel-styles='{"numberFormat":{"formatCode":"PhpSpreadsheet_Style_NumberFormat::FORMAT_CURRENCY_USD"}}'>1520.20</td>
<td _excel-styles='{"format": "@money"}'>25</td>
<td _excel-styles='{"format": "#’##0.00"}'>1520.20</td>
</tr>

<tr>
<td></td>
<td></td>
<!-- Set column dimensions -->
<td _excel-dimensions='{"column":{"width":20}, "row":{"rowHeight":50}}'>No need to fill all cells.</td>
<td _excel-dimensions='{"column":{"autoSize":"true"}}'>And no need to care about how many cells there are in each row.</td>
</tr>

<!-- Merge cells -->
<tr>
<td colspan="3" rowspan="3">Merge cells!</td>
<td _excel-styles='{"width": 100, "height": 50}'>No need to fill all cells.</td>
<td _excel-styles='{"width": "auto"}'>And no need to care about how many cells there are in each row.</td>
<td>Ich bin ein Ümläøut.</td>
</tr>

</table>
28 changes: 11 additions & 17 deletions lib/HtmlPhpExcel/Elements/BaseElement.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
<?php

namespace Ticketpark\HtmlPhpExcel\Elements;
declare(strict_types=1);

use Doctrine\Common\Collections\ArrayCollection;
namespace Ticketpark\HtmlPhpExcel\Elements;

abstract class BaseElement
{
/**
* A collection of attributes of this cell
*
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $attributes;

public function __construct()
{
$this->attributes = new ArrayCollection();
}
private array $attributes = [];

public function addAttribute(string $key, string $value): void
{
$this->attributes->set($key, $value);
$this->attributes[$key] = $value;
}

public function getAttribute($key): ?string
public function getAttribute(string $key): ?string
{
return $this->attributes->get($key);
if (!isset($this->attributes[$key])) {
return null;
}

return $this->attributes[$key];
}
}
}
21 changes: 6 additions & 15 deletions lib/HtmlPhpExcel/Elements/Cell.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
<?php

declare(strict_types=1);

namespace Ticketpark\HtmlPhpExcel\Elements;

class Cell extends BaseElement implements Element
{
/**
* The value of a table cell
*
* @var string
*/
private $value;

/**
* Flag whether the cell is a header cell (<th>)
*
* @var bool
*/
private $isHeader = false;
private ?string $value = null;
private bool $isHeader = false;

public function setValue(string $value): void
{
$this->value = $value;
}

public function getValue(): string
public function getValue(): ?string
{
return $this->value;
}
Expand All @@ -37,4 +28,4 @@ public function isHeader(): bool
{
return $this->isHeader;
}
}
}
28 changes: 9 additions & 19 deletions lib/HtmlPhpExcel/Elements/Document.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
<?php

namespace Ticketpark\HtmlPhpExcel\Elements;
declare(strict_types=1);

use Doctrine\Common\Collections\ArrayCollection;
namespace Ticketpark\HtmlPhpExcel\Elements;

class Document
{
/**
* A collection of tables within the document
*
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $tables;

/**
* Constructor
*/
public function __construct()
{
$this->tables = new ArrayCollection();
}
private array $tables = [];

public function addTable(Table $table): void
{
$this->tables->add($table);
$this->tables[] = $table;
}

public function getTables(): ArrayCollection
/**
* @return array<Table>
*/
public function getTables(): array
{
return $this->tables;
}
}
}
4 changes: 3 additions & 1 deletion lib/HtmlPhpExcel/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Ticketpark\HtmlPhpExcel\Elements;

interface Element {};
interface Element
{
};
Loading
Loading