lekoala/spread-compat
Composer 安装命令:
composer require lekoala/spread-compat
包简介
Easily manipulate PhpSpreadsheet, OpenSpout and League CSV
README 文档
README
Easily manipulate PhpSpreadsheet, OpenSpout, League CSV and Baresheet
Why use this ?
Importing/exporting csv data is a very common task in web development. While it's a very efficient format, it's also somewhat difficult for end users that are used to Excel. This is why you often end up accepting also xlsx or ods format as a import/export target.
Ideally, importing single sheets of csv, excel or ods should be just a matter of changing an adapter. Thankfully, this package does just this :-)
Supported packages
Baresheet (Native): very fast csv, xlsx and ods import/export, but limited features. Can read/output streams. It is used as our default native adapter. https://github.com/lekoala/baresheet
OpenSpout: fast csv, excel (xlsx) and ods import/export https://github.com/openspout/openspout
League CSV: very fast csv import/export. Can read streams. https://github.com/thephpleague/csv
PhpSpreadsheet: slow excel (xls, xlsx) and ods and csv import/export, but more features https://github.com/PHPOffice/PhpSpreadsheet
SimpleXLSX: very fast excel import/export https://github.com/shuchkin/simplexlsx https://github.com/shuchkin/simplexlsxgen
This package will prioritize installed library, by order of performance. You can also pick your preferred default adapter for each format like this:
SpreadCompat::$preferredCsvAdapter = SpreadCompat::BARESHEET; // or SpreadCompat::NATIVE SpreadCompat::$preferredXlsxAdapter = SpreadCompat::BARESHEET; SpreadCompat::$preferredOdsAdapter = SpreadCompat::BARESHEET;
Using the facade
While you can use individual adapters, it's very likely you don't want to bother too much how your files are read and written. This package provides a simple facade with static methods in order to read and write files.
Please note that read methods return a Generator. If you want an array, you need to use iterator_to_array.
$data = iterator_to_array(SpreadCompat::read('myfile.csv')); // or foreach(SpreadCompat::read('myfile.xlsx') as $row) { // Do something } // or even foreach(SpreadCompat::read('myfile.ods') as $row) { // Do something }
Output to browser
This package includes a simple way to leverage output to browser type of functionnality.
Some adapters allow you to stream directly the response.
SpreadCompat::output('myfile.csv'); exit();
Configure
Using named arguments
This package accepts options using ...opts, this means you can freely use named arguments or pass an array.
$data = iterator_to_array(SpreadCompat::read('myfile.csv', assoc: true)); // or $data = iterator_to_array(SpreadCompat::read('myfile.csv', ...$opts));
Using options object
You can also use the Options class that regroups all available options for all adapters. Unsupported options are ignored.
$options = new Options(); $options->separator = ";"; $data = iterator_to_array(SpreadCompat::read('myfile.csv', $options));
Setting the adapter
Instead of relying on the static variables, you can choose which adapter to use:
$csvData = SpreadCompat::readString($csv, adapter: SpreadCompat::BARESHEET); // or $options = new Options(); $options->adapter = SpreadCompat::NATIVE; $csvData = SpreadCompat::readString($csv, $options);
Security
CSV Formula Injection
When exporting to CSV, cell values starting with =, +, -, @, \t, or \r can be interpreted as formulas by spreadsheet software like Excel. This is known as CSV Formula Injection.
By default, this library does NOT escape these characters to ensure that the data is not altered and remains compatible with other tools that may expect raw data.
If you are generating CSV files for end users to open in Excel and want to protect them from potential formula injection, you should enable the escapeFormulas option:
SpreadCompat::write('myfile.csv', $data, escapeFormulas: true);
This will prepend a single quote (') to any cell value that could be interpreted as a formula.
Worksheets
This package supports only 1 worksheet, as it is meant to be able to replace csv by xlsx or vice versa
Benchmarks
Since we can compare our solutions, there is a built in bench script. You can check the results here
For simple imports/exports, it's very clear that using the Native (Baresheet) adapter is the fastest overall choice.
Stop wasting cpu cycles right now and please use the most efficient adapter :-)
lekoala/spread-compat 适用场景与选型建议
lekoala/spread-compat 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.86k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2023 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「excel」 「spreadsheet」 「xls」 「xlsx」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lekoala/spread-compat 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lekoala/spread-compat 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lekoala/spread-compat 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 23.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 23
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-27