sivolobov94 / fast-excel-writer
轻量级且非常快速的PHP XLSX电子表格写入器
Requires
- php: >=7.4
- ext-json: *
- ext-mbstring: *
- ext-zip: *
- avadim/fast-excel-helper: ^1.0
Requires (Dev)
- ext-fileinfo: *
- avadim/fast-excel-reader: ^2.11
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-19 13:46:39 UTC
README
FastExcelWriter
FastExcelWriter 是 FastExcelPhp 项目 的一部分,该项目包括
- FastExcelWriter - 创建Excel电子表格
- FastExcelReader - 读取Excel电子表格
- FastExcelTemplator - 从XLSX模板生成Excel电子表格
- FastExcelLaravel - 特殊的 Laravel 版本
简介
这个库被设计成轻量级、超级快速,并使用最少的内存。
FastExcelWriter 在XLSX格式(Office 2007+)中创建与Excel兼容的电子表格,支持许多功能
- 接受UTF-8编码的输入
- 多工作表
- 支持货币/日期/数字单元格格式化、公式和活动超链接
- 支持单元格、行、列的大多数样式选项 - 颜色、边框、字体等。
- 您可以设置行的宽度和列的宽度(包括自动宽度计算)
- 您可以在XLSX文件中添加公式、注释和图片
- 支持带/不带密码的工作表和电子表格保护
- 支持页面设置 - 页边距、页面大小
跳转到
- 版本4中的变更
- 简单示例
- 高级示例
- 行的设置
- 列的设置
- 定义命名范围
- 添加注释
- 添加图片
- 工作簿
- 工作表
- 写入
- 样式
- 工作簿和工作表的保护
- FastExcelWriter 与 PhpSpreadsheet 的比较
- 您想支持 FastExcelWriter 吗?
安装
使用 composer 将 FastExcelWriter 安装到您的项目中
composer require Sivolobov/fast-excel-writer
版本4中的变更
- 现在库运行得更快了
- 添加了应用样式的流畅接口
- 新方法和代码重构
使用方法
您可以在下面找到使用示例或 /demo 文件夹中的示例
简单示例
use \avadim\FastExcelWriter\Excel; $data = [ ['2003-12-31', 'James', '220'], ['2003-8-23', 'Mike', '153.5'], ['2003-06-01', 'John', '34.12'], ]; $excel = Excel::create(['Sheet1']); $sheet = $excel->sheet(); // Write heads $sheet->writeRow(['Date', 'Name', 'Amount']); // Write data foreach($data as $rowData) { $rowOptions = [ 'height' => 20, ]; $sheet->writeRow($rowData, $rowOptions); } $excel->save('simple.xlsx');
此外,您还可以将生成的文件下载到客户端(发送到浏览器)
$excel->download('download.xlsx');
高级示例
use \avadim\FastExcelWriter\Excel; $head = ['Date', 'Name', 'Amount']; $data = [ ['2003-12-31', 'James', '220'], ['2003-8-23', 'Mike', '153.5'], ['2003-06-01', 'John', '34.12'], ]; $headStyle = [ 'font' => [ 'style' => 'bold' ], 'text-align' => 'center', 'vertical-align' => 'center', 'border' => 'thin', 'height' => 24, ]; $excel = Excel::create(['Sheet1']); $sheet = $excel->sheet(); // Write the head row (sets style via array) $sheet->writeHeader($head, $headStyle); // The same result with new fluent interface $sheet->writeHeader($head) ->applyFontStyleBold() ->applyTextAlign('center', 'center') ->applyBorder(Style::BORDER_STYLE_THIN) ->applyRowHeight(24); // Sets columns options - format and width (the first way) $sheet ->setColFormats(['@date', '@text', '0.00']) ->setColWidths([12, 14, 5]); // The seconds way to set columns options $sheet // column and options ->setColOptions('A', ['format' => '@date', 'width' => 12]) // column letter in lower case ->setColOptions('b', ['format' => '@text', 'width' => 24]) // column can be specified by number ->setColOptions(3, ['format' => '0.00', 'width' => 15, 'color' => '#090']) ; // The third way - all options in multilevel array (first level keys point to columns) $sheet ->setColOptions([ 'A' => ['format' => '@date', 'width' => 12], 'B' => ['format' => '@text', 'width' => 24], 'C' => ['format' => '0.00', 'width' => 15, 'color' => '#090'], ]); $rowNum = 1; foreach($data as $rowData) { $rowOptions = [ 'height' => 20, ]; if ($rowNum % 2) { $rowOptions['fill-color'] = '#eee'; } $sheet->writeRow($rowData, $rowOptions); } $excel->save('simple.xlsx');
行的设置
您可以在使用 writeRow() 函数时,通过第二个参数设置行选项(样式和高度)。注意,在这种情况下,这些样式将只应用于写入数据的行中的单元格
// Write row data and set height $rowOptions = [ 'fill-color' => '#fffeee', 'border' => 'thin', 'height' => 28, ]; $sheet->writeRow(['aaa', 'bbb', 'ccc'], $rowOptions);
另一种实现相同结果的方法
$sheet->writeRow(['aaa', 'bbb', 'ccc', null, 'eee']) ->applyFillColor('#fffeee') ->applyBorder('thin') ->applyRowHeight(28);
您可以设置行的宽度和可见性
// Set height of row 2 to 33 $sheet->setRowHeight(2, 33); // Set height of rows 3,5 and 7 to 33 $sheet->setRowHeight([3, 5, 7], 33); // Set heights of several rows $sheet->setRowHeights([1 => 20, 2 => 33, 3 => 40]); // Hide row 8 $sheet->setRowVisible(8, false); // Other way $sheet->setRowHidden(8); // Hide rows 9, 10, 11 $sheet->setRowVisible([9, 10, 11], false); // Show row 10 $sheet->setRowVisible(10, true);
重要:您只能在至少与当前行号相同或更高的行上使用setRowXX()函数。请参阅按行写入与直接写入。因此,以下代码将抛出错误“行号必须大于已写入的行”
$sheet = $excel->sheet(); // Write row 1 $sheet->writeRow(['aaa1', 'bbb1', 'ccc1']); // Write row 2 $sheet->writeRow(['aaa2', 'bbb2', 'ccc2']); // Try to set height of previous row 1 $sheet->setRowHeight(1, 33);
列的设置
列宽可以通过几种方式设置
// Set width of column D to 24 $this->setColWidth('D', 24); $this->setColOptions('D', ['width' => 24]); // Set auto width $this->setColWidth('D', 'auto'); $this->setColWidthAuto('D'); $this->setColOptions('D', ['width' => 'auto']); // Set width of specific columns $sheet->setColWidths(['B' => 10, 'C' => 'auto', 'E' => 30, 'F' => 40]); // Set width of columns from 'A' $sheet->setColWidths([10, 20, 30, 40], 24); $colOptions = [ 'B' => ['width' => 10], 'C' => ['width' => 'auto'], 'E' => ['width' => 30], 'F' => ['width' => 40], ]; $sheet->setColOptions($colOptions);
您可以定义列的最小宽度。请注意,最小值具有更高的优先级
// Set minimum width to 20 $this->setColMinWidth('D', 20); // The value 10 will not be set because it is less than the minimum value $this->setColWidth('D', 10); // But width 30 will be set $this->setColWidth('D', 30); // The column width will be set to the width of the content, but not less than 20 $this->setColWidthAuto('D');
定义命名范围
FastExcelWriter支持命名范围,但不支持命名公式。一个命名范围提供了一个对单元格或单元格范围的名称引用。所有命名范围都添加到工作簿中,因此所有名称都必须是唯一的,但您可以在工作表或工作簿中定义命名范围。
此外,范围名称必须以字母或下划线开头,不能包含空格,且长度不超过255个字符。
$excel = Excel::create(); $excel->setFileName($outFileName); $sheet = $excel->sheet(); // Named a single cell $sheet->addNamedRange('B2', 'cell_name'); // Named range in a sheet $sheet->addNamedRange('c2:e3', 'range_name'); // Add named range in a workbook (sheet name required) $excel->addNamedRange('Sheet1!A1:F5', 'A1_F5'); // You can define name using applyNamedRange() $sheet->writeCell(1000)->applyNamedRange('Value'); $sheet->writeCell(0.12)->applyNamedRange('Rate'); // Add the formula using names $sheet->writeCell('=Value*Rate');
添加注释
Excel当前有两种类型的注释 - 注释和笔记(请参阅线程化注释与笔记的区别)。笔记是Excel中的旧式注释(浅黄色背景上的文本)。您可以使用addNote()方法将笔记添加到任何单元格
$sheet->writeCell('Text to A1'); $sheet->addNote('A1', 'This is a note for cell A1'); $sheet->writeCell('Text to B1')->addNote('This is a note for B1'); $sheet->writeTo('C4', 'Text to C4')->addNote('Note for C1'); // If you specify a range of cells, then the note will be added to the left top cell $sheet->addNote('E4:F8', "This note\nwill added to E4"); // You can split text into multiple lines $sheet->addNote('D7', "Line 1\nLine 2");
您可以更改一些笔记选项。笔记允许的选项有
- 宽度 - 默认值为
'96pt' - 高度 - 默认值为
'55.5pt' - 填充颜色 - 默认值为
'#FFFFE1' - 显示 - 默认值为
false
$sheet->addNote('A1', 'This is a note for cell A1', ['width' => '200pt', 'height' => '100pt', 'fill_color' => '#ffcccc']); // Parameters "width" and "height" can be numeric, by default these values are in points // The "fill_color" parameter can be shortened $noteStyle = [ 'width' => 200, // equivalent to '200pt' 'height' => 100, // equivalent to '100pt' 'fill_color' => 'fcc', // equivalent to '#ffcccc' ]; $sheet->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle); // This note is visible when the Excel workbook is displayed $sheet->addNote('C8', 'This note is always visible', ['show' => true]);
添加图片
// Insert an image to the cell A1 $sheet->addImage('A1', 'path/to/file'); // Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally) $sheet->addImage('B2', 'path/to/file', ['width' => 150]); // Set height to 150 pixels (with will change proportionally) $sheet->addImage('C3', 'path/to/file', ['height' => 150]); // Set size in pixels $sheet->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150]);
FastExcelWriter与PhpSpreadsheet
PhpSpreadsheet是一个完美的库,具有阅读和写入多种文档格式的出色功能。FastExcelWriter只能写入,并且仅限于XLSX格式,但它非常快,并且内存使用量最小。
FastExcelWriter:
- 7-9倍更快
- 内存使用量减少8-10倍
- 支持写入100K+行的大型工作表
PhpSpreadsheet(P)和FastExcelWriter(F)的基准测试,无样式的电子表格生成
您想支持 FastExcelWriter 吗?
如果您觉得这个包很有用,您可以通过捐赠给我一杯咖啡来支持我
- USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
- USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
- ETH 0x5244519D65035aF868a010C2f68a086F473FC82b
或者直接在GitHub上给我一个star :)