PHP Spreadsheet extension to the export excel file from excel template
PHP Spreadsheet extension to the export excel file from excel template
Инструкция на русском языке (Russian)
It's PHP Spreadsheet extension that allows you to export excel files from an excel template. Using the extension you don’t need to create excel files from scratch using code, set styles and so on.
Demo screenshot:
There is a simplest example of how this might look (using less code). Suppose we have an excel file with the following template variables:
The code will be as follows:
use alhimik1986\PhpExcelTemplator\PhpExcelTemplator;
require_once('vendor/autoload.php'); // if you don't use framework
PhpExcelTemplator::saveToFile('./template.xlsx', './exported_file.xlsx', [
'{current_date}' => date('d-m-Y'),
'{department}' => 'Sales department',
]);As a result, we get:
Using this extension, we just create a template file with the styles we need and specify template variables in it. In the code, we just pass the parameters to template variables.
Features demo and usage examples are given in the folder "samples".
$ composer require alhimik1986/php-excel-templatorThe rules can be any, but I can offer my recommendation for naming template variables:
To do this, instead of a one-dimensional array, insert a two-dimensional one as follows:
$param['[[var_name]]'] = [['text 1', 'text 2', 'text 3']];In the example above, the minimum code without setters was used. The data types (for example: a string, a one-dimensional array, or a two-dimensional array) in this code is automatically recognized and the necessary setter is chose. But if we want to use a specific setter, the same code will look like this:
use alhimik1986\PhpExcelTemplator\PhpExcelTemplator;
use alhimik1986\PhpExcelTemplator\params\ExcelParam;
use alhimik1986\PhpExcelTemplator\params\CallbackParam;
use alhimik1986\PhpExcelTemplator\setters\CellSetterStringValue;
require_once('vendor/autoload.php'); // if you don't use framework
$params = [
'{current_date}' => new ExcelParam(CellSetterStringValue::class, date('d-m-Y')),
'{department}' => new ExcelParam(CellSetterStringValue::class, 'Sales department'),
];
PhpExcelTemplator::saveToFile('./template.xlsx', './exported_file.xlsx', $params);At the moment the extension has 3 kinds of setters:
You ask, what for specify setters explicitly?
Examples of code that uses all kinds of setters are listed in the folder "samples".
In most cases to use the setters explicitly is not so convenient. I suppose you want to use minimum code. Therefore, I made it possible to set styles without using setters:
…There are special templates, which require to insert the whole row, and not insert cell with shifting down. Moreover, it's required to merge cells, as well as the cell in which there was a template variable.
For these templates, a special setter has been created: CellSetterArrayValueSpecial. Examples of code that uses it is given in folder: samples/8_special_template.
The following are possible events and an explanation of why they can be applied:
…No open issues yet, or sync has not completed.