staffim / spreadsheet-builder
Small library for PhpSpreadsheet that helps to build xlsx tables by configuring data columns
Installs: 4 267
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 2
Open Issues: 2
Type:stable
Requires
- php: ^7.2|>=8.0
- ext-dom: *
- ext-json: *
- ext-mbstring: *
- phpoffice/phpspreadsheet: ^1.9
Requires (Dev)
- escapestudios/symfony2-coding-standard: ^3.10
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-11-18 20:07:50 UTC
README
Small library for PhpSpreadsheet that helps to build xlsx tables by configuring data columns.
Installation
composer require staffim/spreadsheet-builder
Examples
Creating worksheet builder
<?php namespace Acme\Xlsx; use Staffim\SpreadsheetBuilder\AbstractWorksheetBuilder; use PhpOffice\PhpSpreadsheet\Style\Alignment; class UsersListWorksheetBuilder extends AbstractWorksheetBuilder { public function getTableTitle(iterable $data): string { return sprintf('Users list at %s', (new \DateTime())->format('d.m.Y')); } public function getWorksheetTitle(iterable $data): string { return 'Users list'; } protected function getColumnsSettings(iterable $data): array { return [ [ 'title' => 'ID', 'value' => static function (User $user) { return $user->getId(); }, 'width' => 5, ], [ 'title' => 'Email', 'value' => static function (User $user) { return $user->getEmail(); }, 'width' => 20, ], [ 'title' => 'About', 'value' => static function (User $user) { return $user->getAbout(); }, 'width' => 25, 'style' => [// all available phpspreadsheet's styles 'alignment' => [ 'wrapText' => true, 'vertical' => Alignment::VERTICAL_TOP, ], ], ], ]; } }
building whole spreadsheet
use Staffim\SpreadsheetBuilder\Builder; /// .... $builder = new Builder([ new \Acme\Xlsx\UsersListWorksheetBuilder(), ]); $users = $usersManager->fetchUsers(); $spreadsheet = $builder->build([$users]);
building from template xlsx with placeholders (this example replaced {title}, {content}, {foo} placeholders to $data array values in A1:E2 range of cells)
template before
$data = [
'title' => 'Test title',
'content' => 'Test content',
'author' => 'Test author',
'foo' => 'Foo',
'bar' => 'Bar',
'date' => '20.02.2002',
];
$builder = new Builder([
new TemplateWorksheetBuilder('/path/from/template.xlsx', 'payment', 'A1:E2'),
]);
$spreadsheet = $builder->build([$data]);
Result after
Working with html
Use \Staffim\SpreadsheetBuilder\RichTextToHtmlConverter
for converting html to
RichText
or vice versa:
$converter = new \Staffim\SpreadsheetBuilder\RichTextToHtmlConverter( [ new BoldConverter(), new ItalicConverter(), new UnderlineConverter(), new ColorConverter(), ] ); }; $html = '<span style="color: brown; font-weight: bold">bold<br/></span> <i style="color: #ffcc01">italic</i><br/> <b style="text-decoration: underline">underline111</b> <span style="font-weight: bold; color: #AA0000">bold red</span>'; $richText = $converter->convertFromHtml($html);
See tests for more examples