A wrapper for the PhpSpreadsheet and Spout libraries to help you quickly build Excel reports.
- Silex ~2.x
$app['builder.default'] = 'spout'; // or 'phpspreadsheet'
$app['builder.cache_dir'] = '/var/cache';
$app->register(new BuilderServiceProvider());
// --- OR ---
$app->register(
new BuilderServiceProvider(),
[
'builder.default' => 'phpspreadsheet',
'builder.cache_dir' => '/var/cache',
]
);
$builder = $app['builder'];
$reportArray = [
'headers' => [
'Column 1',
'Column B',
],
'rows' => [
[
'Some Data',
'Some Other Data',
],
[
'Some Data 2',
'Some Other Data 2',
],
],
];
$builder->setSheets([$reportArray]);
$builder->setCreator('App Name');
$builder->setTitle('My Spreadsheet');
$builder->setSheetTitles(['Sheet 1']);
$builder->setDescription('Spreadsheet that contains some data');
$builder->setFilename('App_Name_Spreadsheet_' . $startDate->format('d_m_Y'));
// use generate() to output headers and force file download.
$builder->generate();
// use generateExcel() to create the file.
$builder->generateExcel();
Both Builders are available under the $app['builders']
key, but $app['builder']
will be the default builder you specify.
Accessible via $app['builders']['phpspreadsheet']
.
Accessible via $app['builders']['spout']
.
Feature | PhpSpreadsheet | Spout |
---|---|---|
Cell Alignment | Yes | No |
Auto-sizing Columns | Yes | No |
Custom Column Widths | Yes | No |
Document Properties | Yes | No |
Header Styling | Yes | Yes |
Multiple Sheets | Yes | Yes |
- Allow both caching when building a report as well as short term or perm-caching to a configured location.
Minimal tests can be performed with PHPUnit.
composer tests
or ./vendor/bin/phpunit
composer coverage
These will be available in ./builder_coverage
.