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



