topdelivery / php-excel-templator
PHP 电子表格扩展,用于从模板生成 Excel 文件
Requires
- php: >=5.6.0
- phpoffice/phpspreadsheet: >=1.4
This package is auto-updated.
Last update: 2024-09-24 13:13:17 UTC
README
这是一个 PHP 电子表格扩展,允许您从 Excel 模板导出 Excel 文件。使用此扩展,您不需要从头开始使用代码创建 Excel 文件,设置样式等。
演示截图
简单示例
以下是一个最简单的示例(使用更少的代码)。假设我们有一个包含以下模板变量的 Excel 文件
代码将如下所示
use topdelivery\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 topdelivery/php-excel-templator
模板变量命名规则
规则可以是任何,但我可以提供关于命名模板变量的建议
- {var_name} - 用于字符串值
- [var_name] - 用于一维数组
- [[var_name]] - 用于二维数组
如何插入一维数组,以便创建列而不是行?
为此,不要插入一维数组,而是插入以下二维数组
$param['[[var_name]]'] = [['text 1', 'text 2', 'text 3']];
使用设置器
在上面的示例中,使用了没有设置器的最少代码。此代码中的数据类型(例如:字符串、一维数组或二维数组)将自动识别并选择必要的设置器。但是,如果我们想使用特定的设置器,则相同的代码将如下所示
use topdelivery\PhpExcelTemplator\PhpExcelTemplator;
use topdelivery\PhpExcelTemplator\params\ExcelParam;
use topdelivery\PhpExcelTemplator\params\CallbackParam;
use topdelivery\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 topdelivery\PhpExcelTemplator\PhpExcelTemplator;
use topdelivery\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);