avadim/fast-excel-writer

轻量级且非常快速的 PHP XLSX 电子表格写入器


README

GitHub Release Packagist Downloads GitHub License Static Badge

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

简介

此库设计为轻量级、超级快速,且占用内存最小。

FastExcelWriter 在 XLSX 格式(Office 2007+)中创建与 Excel 兼容的电子表格,支持许多功能

  • 接受 UTF-8 编码的输入
  • 多个工作表
  • 支持货币/日期/数字单元格格式、公式和活动超链接
  • 支持单元格、行、列的多数样式选项 - 颜色、边框、字体等。
  • 您可以设置行的宽度和列的高度(包括自动宽度计算)
  • 您可以在 XLSX 文件中添加公式、注释和图片
  • 支持带/不带密码的工作簿和表保护
  • 支持页面设置 - 页边距、页面大小
  • 插入多个图表

跳转到

安装

使用 composerFastExcelWriter 安装到您的项目中

composer require avadim/fast-excel-writer

版本 6 的变更

  • 数据验证支持

版本 5 的变更

  • 总体新闻是图表支持

版本 5.8 的重要变更

在 v.5.8 之前

$sheet->writeCell(12345); // The number 12345 will be written into the cell
$sheet->writeCell('12345'); // The number 12345 will also be written here

在 5.8 版本及以后

$sheet->writeCell(12345); // The number 12345 will be written into the cell
$sheet->writeCell('12345'); // Here the string '12345' will be written into the cell

如果您想保留以前的向后兼容行为,请在创建工作簿时使用选项 'auto_convert_number'。

$excel = Excel::create(['Sheet1'], ['auto_convert_number' => true]);
$sheet = $excel->sheet();
$sheet->writeCell('12345'); // String '12345' will be automatically converted to a number

版本 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');

添加注释

目前Excel中有两种注释类型 - 注释批注(见线程注释和批注之间的区别)。批注是Excel中的旧式注释(浅黄色背景上的文本)。您可以使用方法 addNote() 将批注添加到任何单元格

$sheet1->writeCell('Text to A1');
$sheet1->addNote('A1', 'This is a note for cell A1');

$sheet1->writeCell('Text to B1')->addNote('This is a note for B1');
$sheet1->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
$sheet1->addNote('E4:F8', "This note\nwill added to E4");

// You can split text into multiple lines
$sheet1->addNote('D7', "Line 1\nLine 2");

您还可以更改一些批注选项。批注允许的选项有

  • 宽度 - 默认值是 '96pt'
  • 高度 - 默认值是 '55.5pt'
  • 填充颜色 - 默认值是 '#FFFFE1'
  • 显示 - 默认值是 false
$sheet1->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'
];
$sheet1->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle);

// This note is visible when the Excel workbook is displayed
$sheet1->addNote('C8', 'This note is always visible', ['show' => true]);

此外,您还可以在批注中使用富文本

$richText = new \avadim\FastExcelWriter\RichText('here is <c=f00>red</c> and <c=00f>blue</c> text');
$sheet1->addNote('C8', $richText);

有关使用富文本的更多信息,请参阅此处:使用富文本

添加图片

// Insert an image to the cell A1
$sheet1->addImage('A1', 'path/to/file');

// Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally)
$sheet1->addImage('B2', 'path/to/file', ['width' => 150]);

// Set height to 150 pixels (with will change proportionally)
$sheet1->addImage('C3', 'path/to/file', ['height' => 150]);

// Set size in pixels
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150]);

// Add hyperlink to the image
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150, 'hyperlink' => 'https://www.google.com/']);

共享字符串

默认情况下,字符串会直接写入工作表。这会略微增加文件大小,但会加快数据写入速度并节省内存。如果您希望字符串写入共享字符串xml,则需要使用 'shared_string' 选项。

$excel = Excel::create([], ['shared_string' => true]);

FastExcelWriterPhpSpreadsheet

PhpSpreadsheet 是一个完美的库,具有许多出色的功能,用于读取和写入多种文档格式。 FastExcelWriter 只能写入,并且只支持XLSX格式,但它写入非常快,并且内存使用量最小。

FastExcelWriter:

  • 7-9倍快
  • 内存使用量减少了8-10倍
  • 支持写入大型100K+行电子表格

PhpSpreadsheet(P)和FastExcelWriter(F)的基准测试,无样式电子表格生成

您想支持 FastExcelWriter 吗?

如果您发现这个包有用,您可以支持并捐助我一杯咖啡

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

或者,只需在GitHub上给我一个星标 :)