PHPackages                             kaxiluo/php-excel-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. kaxiluo/php-excel-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

kaxiluo/php-excel-template
==========================

Generating Excel files from templates, provide excel template variable rendering

1.1.0(3y ago)7121↓100%MITPHPPHP &gt;=7.1.3

Since Mar 17Pushed 3y ago1 watchersCompare

[ Source](https://github.com/kaxiluo/php-excel-template)[ Packagist](https://packagist.org/packages/kaxiluo/php-excel-template)[ RSS](/packages/kaxiluo-php-excel-template/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (3)Used By (0)

PHP Excel Template
==================

[](#php-excel-template)

从模板生成Excel文件，支持多变量的渲染，自定义渲染行为，设置回调函数。

演示
--

[](#演示)

[![Example](https://camo.githubusercontent.com/c0126f6b2ab1baa094a3144b6dd89805ad0fcc61cf0a31f15129c4fe13088970/68747470733a2f2f6b786c65722e6f73732d636e2d7368616e676861692e616c6979756e63732e636f6d2f6769746875622f7068702d657863656c2d74656d706c6174652d6578616d706c652e706e67)](https://camo.githubusercontent.com/c0126f6b2ab1baa094a3144b6dd89805ad0fcc61cf0a31f15129c4fe13088970/68747470733a2f2f6b786c65722e6f73732d636e2d7368616e676861692e616c6979756e63732e636f6d2f6769746875622f7068702d657863656c2d74656d706c6174652d6578616d706c652e706e67)

演示代码如下:

```
use Kaxiluo\PhpExcelTemplate\CellVars\CallbackContext;
use Kaxiluo\PhpExcelTemplate\CellVars\CellArray2DVar;
use Kaxiluo\PhpExcelTemplate\CellVars\CellArrayVar;
use Kaxiluo\PhpExcelTemplate\CellVars\RenderDirection;
use Kaxiluo\PhpExcelTemplate\PhpExcelTemplate;

$data = [
    ['个人任务执行情况', '完成X系统开发', '40', '35', '任务延期一次'],
    ['技术贡献', '主持基础设施项目建设', '30', '30', ''],
    ['素质与能力', '认真负责，积极工作', '20', '20', ''],
    ['防疫工作', '严格执行防疫措施，按要求填写防疫信息统计表', '10', '10', ''],
];
$items = new CellArray2DVar(
    $data,
    true,
    false,
    function (CallbackContext $context) use ($data) {
        if ($context->getLoopColKey() === 3) {
            if ($context->getValue() < $data[$context->getLoopRowKey()][2]) {
                $context->getStyle()->getFont()->getColor()->setARGB('FFFF0000');
            }
        }
    }
);

// 模板变量定义
$vars = [
    'username' => 'lyy',
    'department' => 'IT中心',
    'dateRange' => '2022-03-01 - 2022-03-31',
    'leader' => 'Tim',
    'items' => $items,
    'totalScore' => '=SUM(D5:D' . (4 + count($data)) . ')',
    'x' => new CellArrayVar(['Peace', 'and', 'Love'], RenderDirection::DOWN, false),
];

// 保存文件
PhpExcelTemplate::save('./example-kpi.xlsx', './example-kpi-output.xlsx', $vars);
// 或者浏览器下载
// PhpExcelTemplate::download('./example-kpi.xlsx', 'example-kpi-output.xlsx', $vars);
```

安装
--

[](#安装)

[Packagist](https://packagist.org/packages/kaxiluo/php-excel-template)

```
composer require kaxiluo/php-excel-template
```

功能
--

[](#功能)

- 字符串变量渲染
- 一维数组变量渲染，自定义渲染方向（向下的行或向右的列）、是否插入新的行或列
- 二维数组变量渲染，自定义向下是否插入新的行、向右是否插入新的列
- 设置回调函数，定制渲染样式或其他特殊行为

### 模板变量说明

[](#模板变量说明)

模板中的变量名只允许特定字符（字母数字-\_.）； 若变量的值包含了声明变量的字符串，本程序不会重复渲染，将正常的输出。

#### 字符串变量（CellStringVar）

[](#字符串变量cellstringvar)

1）在模板中使用`{yourStringVarName}`声明字符串变量

2）用法如下：

```
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);
```

#### 一维数组变量（CellArrayVar）

[](#一维数组变量cellarrayvar)

1）在模板中使用`[yourArrayVarName]`声明一维数组变量

2）用法如下：

```
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);
```

#### 二维数组变量（CellArray2DVar）

[](#二维数组变量cellarray2dvar)

1）在模板中使用`[[yourArray2DVarName]]`声明二位数组变量

2）用法如下：

```
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);
```

其他
--

[](#其他)

如果Excel模板中同一个行上的多个变量，均定义为要插入新的行，本程序会自动处理需要插入的最大行数，不会插入额外的行；列同理。用户需要考虑多个变量均为插入新行或列，其产生的相互影响。

###  Health Score

24

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Total

2

Last Release

1387d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c87a33bee8e19583569abb7ed2b6916934ed351c2c6ede56835a322509d4b214?d=identicon)[linyiyong](/maintainers/linyiyong)

---

Top Contributors

[![kaxiluo](https://avatars.githubusercontent.com/u/32103283?v=4)](https://github.com/kaxiluo "kaxiluo (13 commits)")

---

Tags

excelreaderphpexcelphpexceltemplatephpspreadsheettemplate variable render

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kaxiluo-php-excel-template/health.svg)

```
[![Health](https://phpackages.com/badges/kaxiluo-php-excel-template/health.svg)](https://phpackages.com/packages/kaxiluo-php-excel-template)
```

###  Alternatives

[alhimik1986/php-excel-templator

PHP Spreadsheet extension for generating excel files from template

354329.4k1](/packages/alhimik1986-php-excel-templator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
