alhimik1986 / php-excel-templator
PHP电子表格扩展,用于从模板生成Excel文件
v1.0.15
2023-05-03 11:18 UTC
Requires
- php: ^7.4 || ^8.0
- phpoffice/phpspreadsheet: >=1.22
Requires (Dev)
- roave/security-advisories: dev-master
README
这是一个PHP电子表格扩展,允许您从Excel模板导出Excel文件。使用此扩展,您无需从头开始使用代码创建Excel文件、设置样式等。
演示截图
简单示例
以下是这个例子可能的样子(使用更少的代码)。假设我们有一个包含以下模板变量的Excel文件
代码将如下所示
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',
]);
结果,我们得到
使用此扩展,我们只需创建一个包含所需样式的模板文件,并在其中指定模板变量。在代码中,我们只需将参数传递给模板变量。
功能
- 我们可以在一个单元格中插入多个模板变量(如果数据类型是“字符串”)
- 我们可以插入一个一维数组,在这种情况下,将在表格中创建额外的行
- 我们可以插入一个二维数组,在这种情况下,将在表格中创建相应的列和行
- 通过指定单元格的值,您可以在插入数组时更改这些单元格的样式
- 我们可以将相同的模板应用于表格的多个工作表
功能演示和用法示例在“samples”文件夹中。
限制
- 在单个工作表中使用一维或二维数组时可能出现的所谓的副作用。特别是在它不对称地放置的情况下。副作用的一个例子也在“samples”文件夹中给出。
安装
$ composer require alhimik1986/php-excel-templator
模板变量命名规则
规则可以是任何规则,但我可以提供命名模板变量的建议
- {var_name} - 用于字符串值
- [var_name] - 用于一维数组
- [[var_name]] - 用于二维数组
如何插入一维数组,以便在表格中创建列而不是行?
为此,而不是插入一维数组,插入以下二维数组
$param['[[var_name]]'] = [['text 1', 'text 2', 'text 3']];
使用设置器
在上面的示例中,使用了没有设置器的最小代码。此代码中的数据类型(例如:字符串、一维数组或二维数组)会自动识别并选择必要的设置器。但如果我们想使用特定的设置器,相同的代码将如下所示
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);
目前该扩展有3种类型的设置器
- CellSetterStringValue(用于字符串值)
- CellSetterArrayValue(用于一维数组)
- CellSetterArray2DValue(用于二维数组)
您可能想知道为什么要显式指定设置器?
- 首先,因为它很灵活:假设您想创建自己的设置器,使用您自己的算法消除我上面提到的副作用。
- 其次,在每个设置器中,您可以通过回调函数传递一个函数,我们可以在这个函数中更改插入单元格的样式。例如,您需要突出显示这个月销售最好的员工的加粗字体。
所有类型设置器的代码示例列在“samples”文件夹中。
如何不使用设置器设置样式?
在大多数情况下,显式使用设置器并不那么方便。我想您想使用最少的代码。因此,我使您能够在不使用设置器的情况下设置样式
use alhimik1986\PhpExcelTemplator\PhpExcelTemplator;
use alhimik1986\PhpExcelTemplator\params\CallbackParam;
require_once('vendor/autoload.php'); // if you don't use framework
$params = [
'{current_date}' => date('d-m-Y'),
'{department}' => 'Sales department',
'[sales_amount]' => [
'10230',
'45100',
'70500',
],
];
$callbacks = [
'[sales_amount]' => function(CallbackParam $param) {
$amount = $param->param[$param->row_index];
if ($amount > 50000) {
$cell_coordinate = $param->coordinate;
$param->sheet->getStyle($cell_coordinate)->getFont()->setBold(true);
}
},
];
PhpExcelTemplator::saveToFile('./template.xlsx', './exported_file.xlsx', $params, $callbacks);
特殊模板的特殊设置器(CellSetterArrayValueSpecial)
存在特殊的模板,需要插入整行,而不是插入下移的单元格。此外,还需要合并单元格,包括包含模板变量的单元格。
对于这些模板,已创建了一个特殊设置器:CellSetterArrayValueSpecial。使用它的代码示例在文件夹:samples/8_special_template。
事件
以下是可能的事件及其应用原因的说明
$events = [ PhpExcelTemplator::BEFORE_INSERT_PARAMS => function(Worksheet $sheet, array $templateVarsArr) { // fires before inserting values into template variables }, PhpExcelTemplator::AFTER_INSERT_PARAMS => function(Worksheet $sheet, array $templateVarsArr) { // fires after inserting values into template variables. // It is used if you want to insert valuesinto a spreadsheet after columns and rows have been created. // For example, when inserting an array of images. // If you insert images using $callbacks, then the images can shift to the right due to the fact that on the next line the template variable can create additional columns. // See an example: samples/10_images }, PhpExcelTemplator::BEFORE_SAVE => function(Spreadsheet $spreadsheet, IWriter $writer) { // fires before saving to a file. It is used when you need to modify the $writer or $spreadsheet object before saving, for example, $writer->setPreCalculateFormulas(false); }, ]; $callbacks = []; $templateFile = './template.xlsx'; $fileName = './exported_file.xlsx'; $params = [ // ... ]; PhpExcelTemplator::saveToFile($templateFile, $fileName, $params, $callbacks, $events);