aVadim483 / fast-excel-templator

Lightweight and very fast Excel Spreadsheet generator from XLSX-templates in PHP
MIT License
7 stars 1 forks source link
excel php phpexcel spreadsheets

FastExcelTemplator

FastExcelTemplator is a part of the FastExcelPhp Project which consists of

Introduction

FastExcelTemplator can generate Excel-compatible spreadsheets in XLSX format (Office 2007+) from XLSX templates, very quickly and with minimal memory usage. This library is designed to be lightweight, super-fast and requires minimal memory usage.

Features

Installation

Use composer to install FastExcelTemplator into your project:

composer require avadim/fast-excel-templator

Usage

Example of template

demo1-tpl.png

From this template you can get a file like this

demo1-out.png

Step 1 - open template and set replacements

// Open template and set output file
$excel = Excel::template($tpl, $out);
// Get the first sheet
$sheet = $excel->sheet();

$fillData = [
    '{{COMPANY}}' => 'Comp Stock Shop',
    '{{ADDRESS}}' => '123 ABC Street',
    '{{CITY}}' => 'Peace City, TN',
];

// Set replacements of entire cell values for the sheet
// If the value is '{{COMPANY}}', then this value will be replaced,
// but if the value 'Company Name {{COMPANY}}', then this value will not be replaced 
$sheet->fill($fillData);

// Set replacements of any occurring substrings
// If the value is '{{DATE}}' or 'Date: {{DATE}}', then substring '{{DATE}}' will be replaced,
$replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')];
$sheet->replace($fillData);

Step 2 - transfer the top of the sheet and the table headers from the template to the output file

// Transfer rows 1-6 from templates to output file
$sheet->transferRowsUntil(6);

There are 6 rows read from template, the output file also contains 6 lines

demo1-out.png

Step 3 - insert inner table rows

// Get the row number 7 as a template and go to the next row in the template
$rowTemplate = $sheet->getRowTemplate(7);

// Fill row template and insert it into the output
foreach ($allData as $record) {
    $rowData = [
        // In the column A wil be written value from field 'number'
        'A' => $record['number'],
        // In the column B wil be written value from field 'description'
        'B' => $record['description'],
        // And so on...
        'C' => $record['price1'],
        'D' => $record['price2'],
    ];
    $sheet->insertRow($rowTemplate, $rowData);
}

We filled in and inserted rows 7, 8 and 9

demo1-out.png

Step 4 - Now transfer the remaining rows and save file

// Method transferRows() without arguments transfers remaining rows from the template to the output file 
$sheet->transferRows();

// ...
// Save new file
$excel->save();

demo1-out.png

You can find code examples in /demo folder

List of functions

Class Excel

Excel::template($template, $output): Excel -- Open template file

Class Sheet

Class RowTemplateCollection

Do you like FastExcelTemplator?

if you find this package useful you can support and donate to me for a cup of coffee:

Or just give me a star on GitHub :)