avadim/fast-excel-templator
Composer 安装命令:
composer require avadim/fast-excel-templator
包简介
Lightweight and very fast Excel Spreadsheet generator from XLSX-templates in PHP
README 文档
README
FastExcelTemplator is a part of the FastExcelPhp Project which consists of
- FastExcelWriter - to create Excel spreadsheets
- FastExcelReader - to reader Excel spreadsheets
- FastExcelTemplator - to generate Excel spreadsheets from XLSX templates
- FastExcelLaravel - special Laravel edition
Introduction
FastExcelTemplator can generate Excel-compatible spreadsheets in XLSX format (Office 2007+) from XLSX templates, very quickly and with minimal memory usage. This library is designed to be lightweight, super-fast and requires minimal memory usage.
Features
- Supports XLSX format only (Office 2007+) with multiple worksheets
- Transfers from templates to target spreadsheets styles, images, notes
- Replaces the entire cell values and substrings
- You can use any row from a template as row template to insert and replace a row with new values
- The library can read styling options of cells - formatting patterns, colors, borders, fonts, etc.
Installation
Use composer to install FastExcelTemplator into your project:
composer require avadim/fast-excel-templator
Templates Usage
Example of template
From this template you can get a file like this
Step 1 - open template and set replacements
// Open template and set output file $excel = Excel::template($tpl, $out); // Get the first sheet $sheet = $excel->sheet(); $fillData = [ '{{COMPANY}}' => 'Comp Stock Shop', '{{ADDRESS}}' => '123 ABC Street', '{{CITY}}' => 'Peace City, TN', ]; // Set replacements of entire cell values for the sheet // If the value is '{{COMPANY}}', then this value will be replaced, // but if the value 'Company Name {{COMPANY}}', then this value will not be replaced $sheet->fill($fillData); // Set replacements of any occurring substrings // If the value is '{{DATE}}' or 'Date: {{DATE}}', then substring '{{DATE}}' will be replaced, $replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')]; $sheet->replace($fillData);
Step 2 - transfer the top of the sheet and the table headers from the template to the output file
// Transfer rows 1-6 from templates to output file $sheet->transferRowsUntil(6);
There are 6 rows read from template, the output file also contains 6 lines
Step 3 - insert inner table rows
// Get the row number 7 as a template and go to the next row in the template $rowTemplate = $sheet->getRowTemplate(7); // Fill row template and insert it into the output foreach ($allData as $record) { $rowData = [ // In the column A wil be written value from field 'number' 'A' => $record['number'], // In the column B wil be written value from field 'description' 'B' => $record['description'], // And so on... 'C' => $record['price1'], 'D' => $record['price2'], ]; $sheet->insertRow($rowTemplate, $rowData); }
We filled in and inserted rows 7, 8 and 9
Step 4 - Now transfer the remaining rows and save file
// Method transferRows() without arguments transfers remaining rows from the template to the output file $sheet->transferRows(); // ... // Save new file $excel->save();
You can find code examples in /demo folder
Modification of Spreadsheets
Use the row() method to read rows, modify them using callback, and write them to the output file.
use avadim\FastExcelTemplator\Excel; $excel = Excel::template($tpl, $out); $sheet = $excel->sheet(); $sheet->rows(function ($sourceRowNum, $targetRowNum, $rowData) { // $rowData is an instance of the RowTemplate // skip the first row if ($sourceRowNum === 1) { return null; } // $rowData // if a value of cell 'A' then break if ($rowData->getValue('A') > 5) { return false; } // write value to cell 'B'; if the cell 'B' does not exist, it will be created $rowData->setValue('B', $rowData->getValue('A') * 2); // return modified row return $rowData; }); $excel->save();
You can add one or more cells to the end of a row in the callback function. The styles and value from the source cell will be copied to the new cell. If you do not explicitly specify a source cell, the last cell in the row will be used as the source.
$sheet->rows(function ($sourceRowNum, $targetRowNum, $rowData) { // Clone the last cell of the row and add them to the end of the row and assign it the value 123 $rowData->appendCell()->withValue(123); return $rowData; }); $sheet->rows(function ($sourceRowNum, $targetRowNum, $rowData) { // Clone the cell 'B' and add them to the end $rowData->appendCell('B'); // Clone the last cell three times $rowData->appendCell(null, 3)->withValues([111, 222, 333]); return $rowData; });
Also, you can clone any cell (with styles and value) to other cell
$sheet->rows(function ($sourceRowNum, $targetRowNum, $rowData) { // Clone the cell 'A' to the cell 'E' and assign it the SUM() $rowData->cloneCell('A', 'E') ->withValues(['=SUM(A' . $targetRowNum . ':E' . $targetRowNum . ')']); return $rowData; });
If you need to remove cells, use the removeCells().
$sheet->rows(function ($sourceRowNum, $targetRowNum, $rowData) { // Clone the cell 'A' to the cell 'E' and assign it the SUM() $rowData->removeCells(['B', 'D']); return $rowData; });
List of Functions
Do you like FastExcelTemplator?
if you find this package useful you can support and donate to me for a cup of coffee:
- USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
- USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
- ETH 0x5244519D65035aF868a010C2f68a086F473FC82b
Or just give me a star on GitHub :)
avadim/fast-excel-templator 适用场景与选型建议
avadim/fast-excel-templator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131.5k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「excel」 「spreadsheet」 「xls」 「xlsx」 「library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 avadim/fast-excel-templator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 avadim/fast-excel-templator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 avadim/fast-excel-templator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 131.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-18




