avadim/fast-excel-templator

PHP 中的轻量级、非常快速的 Excel 工作表生成器,基于 XLSX 模板

v2.1.1 2024-06-28 19:36 UTC

This package is auto-updated.

Last update: 2024-08-28 20:02:32 UTC


README

FastExcelTemplatorFastExcelPhp 项目 的一部分,该项目包括

简介

FastExcelTemplator 可以快速生成与 Excel 兼容的 XLSX 格式(Office 2007+)工作表,从 XLSX 模板生成,使用最少的内存。这个库设计得非常轻量级,超级快速,并且内存使用最少。

特性

  • 仅支持 XLSX 格式(Office 2007+)以及多个工作表
  • 从模板到目标工作表的样式、图片、注释的传输
  • 替换整个单元格值和子字符串
  • 您可以使用模板中的任何行作为行模板,以插入和替换带有新值的行
  • 库可以读取单元格的样式选项 - 格式化模式、颜色、边框、字体等。

安装

使用 composerFastExcelTemplator 安装到您的项目中

composer require avadim/fast-excel-templator

使用方法

模板示例

demo1-tpl.png

从这个模板中,您可以生成这样的文件

demo1-out.png

步骤 1 - 打开模板并设置替换

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

步骤 2 - 将模板中的工作表顶部和表头传输到输出文件

// Transfer rows 1-6 from templates to output file
$sheet->transferRowsUntil(6);

从模板中读取了 6 行,输出文件也包含 6 行

demo1-out.png

步骤 3 - 插入内部表行

// 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);
}

我们填写并插入了第 7、8 和 9 行

demo1-out.png

步骤 4 - 现在传输剩余的行并保存文件

// Method transferRows() without arguments transfers remaining rows from the template to the output file 
$sheet->transferRows();

// ...
// Save new file
$excel->save();

demo1-out.png

代码示例可以在 /demo 文件夹中找到

函数列表

Excel 类

Excel::template($template, $output): Excel -- 打开模板文件

  • sheet(?string $name = null): ?Sheet -- 选择指定的表
  • fill(array $replacement) -- 为所有表设置整个单元格值的替换
  • replace(array $replacement) -- 为所有表中的单元格设置子字符串的替换
  • save(?string $fileName = null, ?bool $overWrite = true): bool -- 保存生成的 XLSX 文件
  • download(string $name = null): void -- 将生成的文件下载到客户端(发送到浏览器)

Sheet 类

  • fill(array $replacement) -- 为表设置整个单元格值的替换
  • replace(array $replacement) -- 为表中的单元格设置子字符串的替换
  • getRowTemplate(int $rowNumber, ?bool $savePointerPosition = false) -- 从行获取模板
  • getRowTemplates(int $rowNumberMin, int $rowNumberMax, ?bool $savePointerPosition = false) -- 获取行模板
  • transferRows(?int $countRows = null, $callback = null) -- 从模板传输行到输出
  • transferRowsUntil(?int $maxRowNum = null, $callback = null) -- 从模板传输行到输出
  • insertRow($rowTemplates, ?array $cellData = [])
  • skipRows(?int $countRows = null)
  • skipRowsUntil(?int $maxRowNum = null)

RowTemplateCollection 类

  • cloneCell(string $colSource, $colTarget, ?bool $checkMerge = false): RowTemplateCollection

你喜欢 FastExcelTemplator 吗?

如果您觉得这个包很有用,您可以支持并捐给我一杯咖啡

  • USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
  • USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
  • ETH 0x5244519D65035aF868a010C2f68a086F473FC82b

或者在GitHub上给我一个Star吧:)