1. Go to this page and download the library: Download kaxiluo/php-excel-template library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Kaxiluo\PhpExcelTemplate\CellVars\CellStringVar;
use Kaxiluo\PhpExcelTemplate\CellVars\CallbackContext;
use Kaxiluo\PhpExcelTemplate\PhpExcelTemplate;
$vars = [
// 默认
'var1' => 'value1',
'var2' => new CellStringVar('value2'),
// 设置回调
'var3' => new CellStringVar('i was red color', function (CallbackContext $context) {
$context->getStyle()->getFont()->getColor()->setARGB('FFFF0000');
}),
];
PhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);
use Kaxiluo\PhpExcelTemplate\CellVars\CellArrayVar;
use Kaxiluo\PhpExcelTemplate\CellVars\CallbackContext;
use Kaxiluo\PhpExcelTemplate\CellVars\RenderDirection;
use Kaxiluo\PhpExcelTemplate\PhpExcelTemplate;
$vars = [
// 默认向下渲染,插入新的行
'var1' => ['x1', 'x2'],
'var2' => new CellArrayVar(['x', 'x']),
'var3' => new CellArrayVar(['x', 'x'], RenderDirection::DOWN, true),
// 向右渲染,插入新的列
'var4' => new CellArrayVar(['x', 'x'], RenderDirection::RIGHT),
// 向右渲染,不插入新的列
'var5' => new CellArrayVar(['x', 'x'], RenderDirection::RIGHT, false),
// 向下渲染,不插入新的行
'var6' => new CellArrayVar(['x', 'x'], RenderDirection::DOWN, false),
// 设置回调
'var7' => new CellArrayVar(
['x7-1', 'x7-2', 'x7-3'],
RenderDirection::RIGHT,
true,
function (CallbackContext $context) {
// $context->getWorksheet()
// $context->getValue()
// 设置第二条数据(x7-2)加粗
if ($context->getLoopColKey() === 1) {
$context->getStyle()->getFont()->setBold(true);
}
}
),
];
PhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);
use Kaxiluo\PhpExcelTemplate\CellVars\CellArray2DVar;
use Kaxiluo\PhpExcelTemplate\CellVars\CallbackContext;
use Kaxiluo\PhpExcelTemplate\PhpExcelTemplate;
$vars = [
// 默认向右-下方渲染,下方插入新的行,右边不插入新的列
'var1' => [['a1', 'b1'], ['a2', 'b2']],
'var2' => new CellArray2DVar([['c1', 'd1'], ['c2', 'd2']]),
'var3' => new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], true, false),
// 插入新的行和列
'var4' => new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], true, true),
// 不插入新的行和列
'var5' => new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], false, false),
// 不插入新的行,插入新的列
'var6' => new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], false, true),
// 设置回调
'var7' => new CellArray2DVar(
[['x', '88'], ['y', '59'], ['z', '95']],
true,
false,
function (CallbackContext $context) {
// 第二列小于60则字体标红
if ($context->getLoopColKey() === 1 && $context->getValue() < 60) {
$context->getStyle()->getFont()->getColor()->setARGB('FFFF0000');
}
}
),
];
PhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.