slam/php-spreadsheet-helper

This package is abandoned and no longer maintained. No replacement package was suggested.

PhpSpreadsheet helper to create organized data table

v0.2.2 2022-02-25 10:40 UTC

README

Latest Stable Version Downloads Integrate Code Coverage Infection MSI

Installation

composer require slam/php-spreadsheet-helper

Usage

use Slam\PhpSpreadsheetHelper as ExcelHelper;

require __DIR__ . '/vendor/autoload.php';

// Being an `iterable`, the data can be any dinamically generated content
// for example a PDOStatement set on unbuffered query
$users = [
    [
        'column_1' => 'John',
        'column_2' => '123.45',
        'column_3' => '2017-05-08',
    ],
    [
        'column_1' => 'Mary',
        'column_2' => '4321.09',
        'column_3' => '2018-05-08',
    ],
];

$columnCollection = new ExcelHelper\ColumnCollection(...[
    new ExcelHelper\Column('column_1',  'User',     10,     new ExcelHelper\CellStyle\Text()),
    new ExcelHelper\Column('column_2',  'Amount',   15,     new ExcelHelper\CellStyle\Amount()),
    new ExcelHelper\Column('column_3',  'Date',     15,     new ExcelHelper\CellStyle\Date()),
]);

$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();

$activeSheet = $spreadsheet->getActiveSheet();
$activeSheet->setTitle('My Users');
$table = new ExcelHelper\Table($activeSheet, 1, 1, 'My Heading', $users);
$table->setColumnCollection($columnCollection);

(new ExcelHelper\TableWriter())->writeTableToWorksheet($table);
(new PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet))->save(__DIR__.'/test.xlsx');

Result:

Example