{"id":19655847,"url":"https://github.com/kaxiluo/php-excel-template","last_synced_at":"2025-04-28T18:32:05.354Z","repository":{"id":57004863,"uuid":"466044071","full_name":"kaxiluo/php-excel-template","owner":"kaxiluo","description":"Generating Excel files from templates, provide excel template variable rendering","archived":false,"fork":false,"pushed_at":"2023-03-16T07:01:19.000Z","size":171,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-09-19T05:40:30.271Z","etag":null,"topics":["excelreader","phpexcel"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaxiluo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-04T08:33:26.000Z","updated_at":"2024-07-30T08:19:54.000Z","dependencies_parsed_at":"2022-08-21T13:50:55.269Z","dependency_job_id":null,"html_url":"https://github.com/kaxiluo/php-excel-template","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"kaxiluo/php-composer-package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Fphp-excel-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Fphp-excel-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Fphp-excel-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaxiluo%2Fphp-excel-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaxiluo","download_url":"https://codeload.github.com/kaxiluo/php-excel-template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224126743,"owners_count":17260102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["excelreader","phpexcel"],"created_at":"2024-11-11T15:24:13.769Z","updated_at":"2024-11-11T15:24:14.479Z","avatar_url":"https://github.com/kaxiluo.png","language":"PHP","readme":"# PHP Excel Template\n\n从模板生成Excel文件，支持多变量的渲染，自定义渲染行为，设置回调函数。\n\n## 演示\n\n![Example](https://kxler.oss-cn-shanghai.aliyuncs.com/github/php-excel-template-example.png)\n\n演示代码如下:\n```php\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CallbackContext;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CellArray2DVar;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CellArrayVar;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\RenderDirection;\nuse Kaxiluo\\PhpExcelTemplate\\PhpExcelTemplate;\n\n$data = [\n    ['个人任务执行情况', '完成X系统开发', '40', '35', '任务延期一次'],\n    ['技术贡献', '主持基础设施项目建设', '30', '30', ''],\n    ['素质与能力', '认真负责，积极工作', '20', '20', ''],\n    ['防疫工作', '严格执行防疫措施，按要求填写防疫信息统计表', '10', '10', ''],\n];\n$items = new CellArray2DVar(\n    $data,\n    true,\n    false,\n    function (CallbackContext $context) use ($data) {\n        if ($context-\u003egetLoopColKey() === 3) {\n            if ($context-\u003egetValue() \u003c $data[$context-\u003egetLoopRowKey()][2]) {\n                $context-\u003egetStyle()-\u003egetFont()-\u003egetColor()-\u003esetARGB('FFFF0000');\n            }\n        }\n    }\n);\n\n// 模板变量定义\n$vars = [\n    'username' =\u003e 'lyy',\n    'department' =\u003e 'IT中心',\n    'dateRange' =\u003e '2022-03-01 - 2022-03-31',\n    'leader' =\u003e 'Tim',\n    'items' =\u003e $items,\n    'totalScore' =\u003e '=SUM(D5:D' . (4 + count($data)) . ')',\n    'x' =\u003e new CellArrayVar(['Peace', 'and', 'Love'], RenderDirection::DOWN, false),\n];\n\n// 保存文件\nPhpExcelTemplate::save('./example-kpi.xlsx', './example-kpi-output.xlsx', $vars);\n// 或者浏览器下载\n// PhpExcelTemplate::download('./example-kpi.xlsx', 'example-kpi-output.xlsx', $vars);\n```\n\n## 安装\n\n[Packagist](https://packagist.org/packages/kaxiluo/php-excel-template)\n```bash\ncomposer require kaxiluo/php-excel-template\n```\n\n## 功能\n\n- 字符串变量渲染\n- 一维数组变量渲染，自定义渲染方向（向下的行或向右的列）、是否插入新的行或列\n- 二维数组变量渲染，自定义向下是否插入新的行、向右是否插入新的列\n- 设置回调函数，定制渲染样式或其他特殊行为\n\n### 模板变量说明\n\n模板中的变量名只允许特定字符（字母数字-_.）；\n若变量的值包含了声明变量的字符串，本程序不会重复渲染，将正常的输出。\n\n#### 字符串变量（CellStringVar）\n1）在模板中使用`{yourStringVarName}`声明字符串变量\n\n2）用法如下：\n```php\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CellStringVar;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CallbackContext;\nuse Kaxiluo\\PhpExcelTemplate\\PhpExcelTemplate;\n\n$vars = [\n    // 默认\n    'var1' =\u003e 'value1', \n    'var2' =\u003e new CellStringVar('value2'),\n    // 设置回调\n    'var3' =\u003e new CellStringVar('i was red color', function (CallbackContext $context) {\n        $context-\u003egetStyle()-\u003egetFont()-\u003egetColor()-\u003esetARGB('FFFF0000');\n    }),\n];\n\nPhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);\n```\n\n#### 一维数组变量（CellArrayVar）\n1）在模板中使用`[yourArrayVarName]`声明一维数组变量\n\n2）用法如下：\n```php\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CellArrayVar;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CallbackContext;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\RenderDirection;\nuse Kaxiluo\\PhpExcelTemplate\\PhpExcelTemplate;\n\n$vars = [\n    // 默认向下渲染，插入新的行\n    'var1' =\u003e ['x1', 'x2'],\n    'var2' =\u003e new CellArrayVar(['x', 'x']),\n    'var3' =\u003e new CellArrayVar(['x', 'x'], RenderDirection::DOWN, true),\n    // 向右渲染，插入新的列\n    'var4' =\u003e new CellArrayVar(['x', 'x'], RenderDirection::RIGHT),\n    // 向右渲染，不插入新的列\n    'var5' =\u003e new CellArrayVar(['x', 'x'], RenderDirection::RIGHT, false),\n    // 向下渲染，不插入新的行\n    'var6' =\u003e new CellArrayVar(['x', 'x'], RenderDirection::DOWN, false),\n    // 设置回调\n    'var7' =\u003e new CellArrayVar(\n        ['x7-1', 'x7-2', 'x7-3'],\n        RenderDirection::RIGHT,\n        true,\n        function (CallbackContext $context) {\n            // $context-\u003egetWorksheet()\n            // $context-\u003egetValue()\n            // 设置第二条数据(x7-2)加粗\n            if ($context-\u003egetLoopColKey() === 1) {\n                $context-\u003egetStyle()-\u003egetFont()-\u003esetBold(true);\n            }\n        }\n    ),\n];\n\nPhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);\n```\n\n#### 二维数组变量（CellArray2DVar）\n1）在模板中使用`[[yourArray2DVarName]]`声明二位数组变量\n\n2）用法如下：\n```php\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CellArray2DVar;\nuse Kaxiluo\\PhpExcelTemplate\\CellVars\\CallbackContext;\nuse Kaxiluo\\PhpExcelTemplate\\PhpExcelTemplate;\n\n$vars = [\n    // 默认向右-下方渲染，下方插入新的行，右边不插入新的列\n    'var1' =\u003e [['a1', 'b1'], ['a2', 'b2']],\n    'var2' =\u003e new CellArray2DVar([['c1', 'd1'], ['c2', 'd2']]),\n    'var3' =\u003e new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], true, false),\n    // 插入新的行和列\n    'var4' =\u003e new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], true, true),\n    // 不插入新的行和列\n    'var5' =\u003e new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], false, false),\n    // 不插入新的行，插入新的列\n    'var6' =\u003e new CellArray2DVar([['a5', 'b5'], ['a6', 'b6']], false, true),\n    // 设置回调\n    'var7' =\u003e new CellArray2DVar(\n        [['x', '88'], ['y', '59'], ['z', '95']],\n        true,\n        false,\n        function (CallbackContext $context) {\n            // 第二列小于60则字体标红\n            if ($context-\u003egetLoopColKey() === 1 \u0026\u0026 $context-\u003egetValue() \u003c 60) {\n                $context-\u003egetStyle()-\u003egetFont()-\u003egetColor()-\u003esetARGB('FFFF0000');\n            }\n        }\n    ),\n];\n\nPhpExcelTemplate::save('/path/to/templateFile.xlsx', '/path/to/outputFile.xlsx', $vars);\n```\n\n## 其他\n如果Excel模板中同一个行上的多个变量，均定义为要插入新的行，本程序会自动处理需要插入的最大行数，不会插入额外的行；列同理。用户需要考虑多个变量均为插入新行或列，其产生的相互影响。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaxiluo%2Fphp-excel-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaxiluo%2Fphp-excel-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaxiluo%2Fphp-excel-template/lists"}