slam/openspout-helper

PhpSpreadsheet helper to create organized data table

v2.4.0 2024-09-25 06:23 UTC

This package is auto-updated.

Last update: 2024-12-21 11:02:02 UTC


README

Latest Stable Version Downloads CI Infection MSI

Installation

composer require slam/openspout-helper

Usage

use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use Slam\OpenspoutHelper 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()),
]);

$XLSXWriter  = WriterEntityFactory::createXLSXWriter();
$XLSXWriter->openToFile(__DIR__.'/test.xlsx');

$activeSheet = $XLSXWriter->getCurrentSheet();
$activeSheet->setName('My Users');
$table = new ExcelHelper\Table($activeSheet, 'My Heading', $users);
$table->setColumnCollection($columnCollection);

(new ExcelHelper\TableWriter())->writeTable($XLSXWriter, $table);
$XLSXWriter->close();

Result:

Example