lezhnev74 / eximport
一个易于验证和导入Excel数据的库
1.0
2016-06-13 14:37 UTC
Requires
- phpoffice/phpexcel: 1.8.*
Requires (Dev)
- phpunit/phpunit: 5.4.*
This package is auto-updated.
Last update: 2024-08-29 04:14:35 UTC
README
Eximporter
面向对象的Excel导入器,具有输入验证功能。它允许您导入任何Excel文件,验证每个单元格,并对有效和无效的单元格执行业务逻辑。
提示:它将自动跳过所有单元格都为空的行。
示例
use Eximporter\Eximporter; use Eximporter\Exceptions\BadFile; $file = "./tests/resources/test_05.xlsx"; try { $importer = new Eximporter($file); $importer->setValidationRules([ // you can set rules by names 'description' => 'required', // you can set manual closures as rules (as an array) 'amount' => ['custom_rule' => function($cellvalue){ return $cell_value > 100; }] // you can add few rules in a row 'title' => [ 'required|regexp:#^[0-9]+$#', [ 'custom_rule_2' => function($cell_value) { return strlen($cell_value)<10; } ] ], ]); // set handlers (closures) to handle each good or bad (validation failed) row $importer->setHandlerForBadRow(function ($row, $bad_cells) { foreach ($bad_cells as $cell_title => $validation_result) { echo $cell_title . " failed validators: " . implode(", ", $validation_result->getFailed()); echo "\n"; // example output: // Amount failed validators: custom1 // Description failed validators: required // ... } }); // set handlers for good rows $importer->setHandlerForGoodRow(function ($row) { // business logic with $row }); // ok let's go $importer->read(); // you can access counters echo $importer->getGoodRowsCount(); echo $importer->getBadRowsCount(); } catch (BadFile $e) { // unable to open this file }
使用方法
此包旨在用于具有Excel导入功能的项目。它允许您轻松添加数据验证层和筛选选项。它底层基于PHPOffice/PHPExcel。
安装
composer require lezhnev74/eximport
需求
- PHP7
- 启用PHP扩展php_zip
- 启用PHP扩展php_xml
- 启用PHP扩展php_gd2(如果未编译进)
- (了解更多)[https://github.com/PHPOffice/PHPExcel/wiki/Developer-Documentation-Prerequisites]
鸣谢
Dmitriy Lezhnev lezhnev.work@gmail.com