eclipxe / xlsxexporter
PHP Office Open XML 电子表格 (xlsx) 导出器
v2.0.0
2023-04-03 08:31 UTC
Requires
- php: >=7.4
- ext-simplexml: *
- ext-zip: *
- eclipxe/engineworks-progress-status: ^2.0.1
Requires (Dev)
- ext-xmlwriter: *
- eclipxe/engineworks-dbal: ^2.3.2
- phpunit/phpunit: ^9.6.5
Suggests
- eclipxe/engineworks-dbal: Export recordsets as worksheets in a workbook
README
PHP Office Open XML 电子表格 (xlsx) 导出器是一个使用 PHP 编写 xlsx 文件的工程。我建议您查看 PHPExcel,它对此类文件有很好的支持。
我创建此项目是因为 PHPExcel 无法满足我的需求。具体来说,我使用此工具将大量数据导出到电子表格文件中,以便最终用户导出和处理。使用 PHPExcel 消耗了大量内存,并引发了 "内存耗尽错误"。
一些具有类似功能的项目,以及我用作参考的项目
工作原理
- 您的主要对象是工作簿。
- 工作簿至少包含 1 个电子表格。
- 每个电子表格(工作表)包含一系列列和一个数据提供者对象。
- 当结构信息(工作簿、工作表、列和提供者)设置完成后,您可以写入 xlsx 文件。
- 每次创建工作表时,首先写入表头,然后写入每行数据。数据通过提供者提取。这样,您不需要将所有数据存储在内存中,可以使用实现提供者接口的 PDO 读取器。
- 数据写入临时文件(包括最终的 zip 文件),因此不会使用大量数据。
安装
使用 composer,运行
composer require eclipxe/xlsxexporter
基本用法示例
<?php use Eclipxe\XlsxExporter; use Eclipxe\XlsxExporter\CellTypes; use Eclipxe\XlsxExporter\Column; use Eclipxe\XlsxExporter\Columns; use Eclipxe\XlsxExporter\Providers\ProviderArray; use Eclipxe\XlsxExporter\Style; use Eclipxe\XlsxExporter\Styles\Format; use Eclipxe\XlsxExporter\WorkBook; use Eclipxe\XlsxExporter\WorkSheet; use Eclipxe\XlsxExporter\WorkSheets; use Eclipxe\XlsxExporter\Exceptions\XlsxException; // create a simple array as example $provider = new ProviderArray([ ['first_name' => 'Charles', 'amount' => 1234.561, 'visit' => strtotime('2014-01-13 13:14:15'), 'check' => 1], ['first_name' => 'Foo', 'amount' => 6543.219, 'visit' => strtotime('2014-12-31 23:59:59'), 'check' => 0], ]); // create some special formats $formatNumber2Decimals = new Style(['format' => ['code' => Format::FORMAT_COMMA_2DECS]]); $formatDateTime = new Style(['format' => ['code' => Format::FORMAT_DATE_YMDHM]]); $formatYesNo = new Style(['format' => ['code' => Format::FORMAT_YESNO]]); // create the workbook with all the information $workbook = new WorkBook( new WorkSheets( new WorkSheet('sheet01', $provider, new Columns( new Column('first_name', 'Name'), new Column('amount', 'Amount', CellTypes::NUMBER, $formatNumber2Decimals), new Column('visit', 'Visit', CellTypes::DATETIME, $formatDateTime), new Column('check', 'Check', CellTypes::BOOLEAN, $formatYesNo), )), ) ); // call the write process try{ XlsxExporter::save($workbook, __DIR__ . '/result.xlsx'); } catch (XlsxException $exception) { echo 'Export error: ', $exception->getMessage(), PHP_EOL; }
贡献
欢迎贡献!请阅读 CONTRIBUTING 以获取详细信息,并别忘了查看 TODO 和 CHANGELOG 文件。
许可证
eclipxe/xlsxexporter
库版权所有 © Carlos C Soto,许可协议为 MIT 许可证(MIT)。有关更多信息,请参阅 LICENSE。