ang3/php-excel-encoder
Composer 安装命令:
composer require ang3/php-excel-encoder
包简介
PHP Excel encoder
README 文档
README
Encode and decode xls/xlsx files and more thanks to the component phpoffice/phpspreadsheet.
Also, this component uses the component Serializer from package
symfony/serializer - Please read the
documentation for more information about serializer usage.
Summary
Installation
Open a command console and execute the following command to download the latest stable version of this bundle:
composer require ang3/php-excel-encoder
If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.
Upgrade
To upgrade from 1.x to 2.x or from 2.x to 3.x, please read the file UPGRADE-2.0.md.
Usage
Create encoder
<?php require_once 'vendor/autoload.php'; use Ang3\Component\Serializer\Encoder\ExcelEncoder; // Create the encoder with default context $encoder = new ExcelEncoder($defaultContext = []);
Context parameters:
ExcelEncoder::AS_COLLECTION_KEY(boolean): Load data as collection [default:true]ExcelEncoder::FLATTENED_HEADERS_SEPARATOR_KEY(string): separator for flattened entries key [default:.]ExcelEncoder::HEADERS_IN_BOLD_KEY(boolean): put headers in bold (encoding only) [default:true]ExcelEncoder::HEADERS_HORIZONTAL_ALIGNMENT_KEY(string): put headers in bold (encoding only:left,centerorright) [default:center]ExcelEncoder::COLUMNS_AUTOSIZE_KEY(boolean): column autosize feature (encoding only) [default:true]ExcelEncoder::COLUMNS_MAXSIZE_KEY(integer): column maxsize feature (encoding only) [default:50]
Encoding
Accepted formats:
ExcelEncoder::XLSExcelEncoder::XLSX
<?php // Create the encoder... // Test data $data = [ // Array by sheet 'My sheet' => [ // Array by rows [ 'bool' => false, 'int' => 1, 'float' => 1.618, 'string' => 'Hello', 'object' => new DateTime('2000-01-01 13:37:00'), 'array' => [ 'bool' => true, 'int' => 3, 'float' => 3.14, 'string' => 'World', 'object' => new DateTime('2000-01-01 13:37:00'), 'array' => [ 'again' ] ], ], ] ]; // Encode data with specific format $xls = $encoder->encode($data, ExcelEncoder::XLSX); // Put the content in a file with format extension for example file_put_contents('my_excel_file.xlsx', $xls);
Decoding
Accepted formats:
ExcelEncoder::XLSExcelEncoder::XLSX
// Create the encoder... // Decode data with no specific format $data = $encoder->decode('my_excel_file.xlsx', ExcelEncoder::XLS); var_dump($data); // Output: // // array(1) { // ["Sheet_0"] => array(1) { // Name of the sheet // [0] => array(15) { // First row // ["bool"] => int(0) // First cell // ["int"] => int(1) // ["float"] => float(1.618) // ["string"] => string(5) "Hello" // ["object.date"] => string(26) "2000-01-01 13:37:00.000000" // ["object.timezone_type"] => int(3) // ["object.timezone"] => string(13) "Europe/Berlin" // ["array.bool"] => int(1) // ["array.int"] => int(3) // ["array.float"] => float(3.14) // ["array.string"] => string(5) "World" // ["array.object.date"] => string(26) "2000-01-01 13:37:00.000000" // ["array.object.timezone_type"] => int(3) // ["array.object.timezone"] => string(13) "Europe/Berlin" // ["array.array.0"] => string(5) "again" // } // } // }
Headers
By default, the encoder loads data as collection: the header name is the key of each data.
Indeed, the default value of context parameter ExcelEncoder::AS_COLLECTION_KEY is true.
To disable this feature, pass the context parameter as false.
$data = $encoder->decode('my_excel_file.xlsx', ExcelEncoder::XLS, [ ExcelEncoder::AS_COLLECTION_KEY => false ]);
// Output:
//
// array(1) {
// ["Sheet_0"] => array(1) { // Name of the sheet
// [0] => array(15) { // Headers row
// 0 => "bool" // First cell
// 1 => "int"
// 2 => "float"
// 3 => "string"
// 4 => "object.date"
// 5 => "object.timezone_type"
// 6 => "object.timezone"
// 7 => "array.bool"
// 8 => "array.int"
// 9 => "array.float"
// 10 => "array.string"
// 11 => "array.object.date"
// 12 => "array.object.timezone_type"
// 13 => "array.object.timezone"
// 14 => "array.array.0"
// },
// [1] => array(15) { // First data row
// [0] => int(0) // First cell
// [1] => int(1)
// [2] => float(1.618)
// [3] => string(5) "Hello"
// [4] => string(26) "2000-01-01 13:37:00.000000"
// [5] => int(3)
// [6] => string(13) "Europe/Berlin"
// [7] => int(1)
// [8] => int(3)
// [9] => float(3.14)
// [10] => string(5) "World"
// [11] => string(26) "2000-01-01 13:37:00.000000"
// [12] => int(3)
// [13] => string(13) "Europe/Berlin"
// [14] => string(5) "again"
// }
// }
// }
Symfony integration
The encoder implements the interfaces of the Serializer component. To enable this encoder, you could just
configure it as service by adding the contents below into the file config/services.yaml:
# config/services.yaml services: # ... Ang3\Component\Serializer\Encoder\ExcelEncoder: ~
Run tests
$ git clone https://github.com/Ang3/php-excel-encoder.git
$ composer install
$ vendor/bin/simple-phpunit
That's it!
ang3/php-excel-encoder 适用场景与选型建议
ang3/php-excel-encoder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84.99k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ang3/php-excel-encoder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ang3/php-excel-encoder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 84.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-13