迅光科技/fast-excel-writer
轻量级且非常快速的PHP XLSX电子表格编写器
Requires
- php: >=8.0
- ext-intl: *
- ext-json: *
- ext-mbstring: *
- ext-zip: *
This package is auto-updated.
Last update: 2024-09-26 18:17:14 UTC
README
跳转到
简介
此库设计轻量级、超快且内存使用最小。
此库创建与Excel兼容的XLSX格式的电子表格(Office 2007+),只支持基本功能
- 接受UTF-8编码的输入
- 多张工作表
- 支持货币/日期/数字单元格格式化,简单公式
- 支持基本的列、行和单元格样式
FastExcelWriter 与 PhpSpreadsheet
PhpSpreadsheet 是一个完美的库,具有阅读和写入许多文档格式的奇妙功能。 FastExcelWriter 只能写入,并且仅限于xlsx格式,但它非常快,内存使用量最小。
FastExcelWriter:
- 快7-9倍
- 内存使用量减少8-10倍
- 支持写入巨大的100K+行电子表格
顺便说一下,也存在 FastExcelReader - https://github.com/aVadim483/fast-excel-reader
安装
使用 composer 将 FastExcelWriter 安装到您的项目中
composer require avadim/fast-excel-writer
您还可以下载包并包含库的自动加载文件
require 'path/to/fast-excel-writer/src/autoload.php';
版本3的更改
- PHP的最小版本是7.4.x
- 所有预定义的格式现在不区分大小写,必须以 '@' 开头('@date', '@money' 等)
- 已删除方法
setColumns(),请改用方法setColOptions() - 方法
writeRow()有第三个参数:writeRow($rowData, ?array $rowOptions, ?array cellOptions) - 您可以设置列的自动宽度
- 支持活动超链接
使用方法
您可以在下面的示例中找到或在使用 /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->getSheet(); // Write heads $sheet->writeRow(['Date', 'Name', 'Amount']); // Write data foreach($data as $rowData) { $rowOptions = [ 'height' => 20, ]; $sheet->writeRow($rowData); } $excel->save('simple.xlsx');
您还可以下载生成的文件发送到客户端(发送到浏览器)
$excel->output('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->getSheet(); $sheet->writeHeader($head, $headStyle); $sheet ->setColFormats(['@date', '@text', '0.00']) ->setColWidths([12, 14, 5]); $rowNum = 1; foreach($data as $rowData) { $rowOptions = [ 'height' => 20, ]; if ($rowNum % 2) { $rowOptions['fill'] = '#eee'; } $sheet->writeRow($rowData); } $excel->save('simple.xlsx');
您还可以下载生成的文件发送到客户端(发送到浏览器)
$excel->output('download.xlsx');
写入单元格值
通常,值是按顺序写入的,单元格一个接一个,行一个接一个。写入单元格会将内部指针移动到行的下一个单元格,写入行会将指针移动到下一行的第一个单元格。
use \avadim\FastExcelWriter\Excel; $excel = Excel::create(); $sheet = $excel->getSheet(); // Sheet::writeCell(mixed value, ?array styles) // Sheet::writeTo(string address, mixed value, ?array styles) // Sheet::nextCell() // Write number to A1 and pointer moves to the next cell (B1) $sheet->writeCell(123); // Write string to B1 (pointer in C1) $sheet->writeCell('abc'); // Pointer moves to the next cell (D1) without value writing $sheet->nextCell(); $style = [ 'color' => '#ff0000', 'format' => '#,##0.00', 'align' => 'center', ]; // Write to D1 value and style $sheet->writeCell(0.9, $style); // Merge cells range $sheet->mergeCells('D1:F2'); // Write to B2 and moves pointer to C2. The pointer can only move from left to right and top to bottom $sheet->writeTo('B2', 'value'); // Merge C3:E3, write value to merged cells and move pointer to F3 $sheet->writeTo('C3:E3', 'other value');
您可以将值写入行
// Sheet::writeHeader(array header, ?array rowStyle) // Sheet::writeRow(array row, ?array rowStyle) // Sheet::nextRow() // Write values to the current row and set format of columns A and B $sheet->writeHeader(['title1' => '@integer', 'title2' => '@date']); $data = [ [184, '2022-01-23'], [835, '1971-12-08'], [760, '1997-05-11'], ]; foreach ($data as $rowData) { $sheet->writeRow($rowData); }
直接写入单元格
如果您需要直接写入单元格,必须定义区域。
$area = $sheet->makeArea('B4:F12'); // Make write area from B4 to F12 $area = $sheet->makeArea('B4:12'); // Make write area from B4 to B12 $area = $sheet->beginArea('B4'); // Make write area from B4 to max column and max row // Set style for single cell of area (new style will replace previous) $area->setStyle('B4', $style1); // Set additional style for single cell of area (new style wil be merged with previous) $area->addStyle('B4', $style2); $area->setStyle('D4:F6', $style2); // Set style for single cell of area $area->setValue('A2:J2', 'This is demo XLSX-sheet', $headerStyle); $area ->setValue('H4', 'Date', ['text-align' => 'right']) ->setValue('I4:J4', date('Y-m-d H:i:s'), ['font-style' => 'bold', 'format' => 'datetime', 'text-align' => 'left']) ;
高度和宽度
// Set height of row 2 to 33 $sheet->setRowHeight(2, 33); // Set heights of several rows $sheet->setRowHeights([1 => 20, 2 => 33, 3 => 40]); // Write row data and set height $sheet->writeRow($rowData, ['height' => 20]); // 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]); $sheet->setColOptions(['B' => ['width' => 10], 'C' => ['width' => 'auto'], 'E' => ['width' => 30], 'F' => ['width' => 40]]); // Set width of columns from 'A' $sheet->setColWidths([10, 20, 30, 40]);
单元格格式
您可以使用简单和高级格式
$excel = new \avadim\FastExcelWriter\Excel(['Formats']); $sheet = $excel->getSheet(); $header = [ 'created' => '@date', 'product_id' => '@integer', 'quantity' => '#,##0', 'amount' => '#,##0.00', 'description' => '@string', 'tax' => '[$$]#,##0.00;[RED]-[$$]#,##0.00', ]; $data = [ ['2015-01-01', 873, 1, 44.00, 'misc', '=D2*0.05'], ['2015-01-12', 324, 2, 88.00, 'none', '=D3*0.15'], ]; $sheet->writeHeader($header); foreach($data as $row) { $sheet->writeRow($row ); } $excel->save('formats.xlsx');
简单单元格格式映射到更高级的单元格格式
基本单元格样式
字体设置
use \avadim\FastExcelWriter\Style; $style = [ Style::FONT => [ Style::FONT_NAME => 'Arial', Style::FONT_SIZE => 14, Style::FONT_STYLE => Style::FONT_STYLE_BOLD, ] ];
边框设置
use \avadim\FastExcelWriter\Style; // simple border style $style1 = [ Style::BORDER => Style::BORDER_THIN ]; // border style with color $style2 = [ Style::BORDER => [ Style::BORDER_ALL => [ Style::BORDER_STYLE => Style::BORDER_THICK, Style::BORDER_COLOR => '#f00', ] ] ]; // extra border style $style3 = [ Style::BORDER => [ Style::BORDER_TOP => Style::BORDER_NONE, Style::BORDER_LEFT => [ Style::BORDER_STYLE => Style::BORDER_THICK, Style::BORDER_COLOR => '#f9009f', ], Style::BORDER_RIGHT => [ Style::BORDER_STYLE => Style::BORDER_MEDIUM_DASH_DOT, Style::BORDER_COLOR => '#f00', ], Style::BORDER_BOTTOM => [ Style::BORDER_STYLE => Style::BORDER_DOUBLE, ], ] ];
其他样式设置
公式
公式必须以 '=' 开始。如果您想将公式作为文本写入,请使用反斜杠。设置区域允许使用国家语言函数名称。您可以在公式中使用A1和R1C1表示法
use \avadim\FastExcelWriter\Excel; $excel = Excel::create(['Formulas']); $sheet = $excel->getSheet(); // Set Russian locale $excel->setLocale('ru'); $headRow = []; $sheet->writeRow([1, random_int(100, 999), '=RC[-1]*0.1']); $sheet->writeRow([2, random_int(100, 999), '=RC[-1]*0.1']); $sheet->writeRow([3, random_int(100, 999), '=RC[-1]*0.1']); $totalRow = [ 'Total', '=SUM(B1:B3)', // English function name '=СУММ(C1:C3)', // You can use Russian function name because the locale is 'ru' ]; $sheet->writeRow($totalRow); $excel->save('formulas.xlsx');
超链接
您可以将URL作为活动超链接插入
// Write URL as simple string (not hyperlink) $sheet->writeCell('https://google.com'); // Write URL as an active hyperlink $sheet->writeCell('https://google.com', ['hyperlink' => true]); // Write text with an active hyperlink $sheet->writeCell('Google', ['hyperlink' => 'https://google.com']);
设置临时文件目录
此库使用临时文件生成XLSX文件。如果不指定,它们将在系统临时目录或当前执行目录中创建。但您可以设置临时文件的目录。
use \avadim\FastExcelWriter\Excel; Excel::setTempDir('/path/to/temp/dir'); // use this call before Excel::create() $excel = Excel::create(); // Or alternative variant $excel = Excel::create('SheetName', ['temp_dir' => '/path/to/temp/dir']);
想要支持FastExcelWriter?
如果您觉得这个包有用,您可以支持并捐赠给我 https://www.paypal.me/VShemarov 或者只要在GitHub上给我星星 :)