alfonsobries/laravel-spreadsheet-importer
Composer 安装命令:
composer require alfonsobries/laravel-spreadsheet-importer
包简介
Sheetjs Laravel Spreadsheet Importer
README 文档
README
This package works together with the xlsx-laravel-spreadsheet-importer CLI tool to quickly import spreadsheets from xlsx files an other compatible formats and then store the values into temporal database tables that are way easier to work with.
Because we use Node instead of PHP we can import larger xlsx files in just a few seconds, and because we will work with a SQL table the operations with the data are considerably faster.
- Includes a command that expect the data according to the cli tool and trigger an observable event with the progress of the import.
- Adds a Trait that add the ability to your model to work with the temporal data as easy as any Eloquent relationship.
- Once the import is finished triggers another observable event so you can know when you can continue with the following operations related with your import.
- Manages multiple files format, see (https://www.npmjs.com/package/xlsx#file-formats)
- Compatible with PostgreSQL as MySQL
Installation
You can install the package via composer:
composer require alfonsobries/laravel-spreadsheet-importer
You also will need the npm package (check alfonsobries/xlsx-laravel-spreadsheet-importer for more info)
npm install @alfonsobries/xlsx-laravel-spreadsheet-importer --save
Optionally publish the config files by running:
php artisan vendor:publish --provider="Alfonsobries\LaravelSpreadsheetImporter\LaravelSpreadsheetImporterServiceProvider" --tag="config"
Configure your model
The files that you will import usually will be associated to one Model, also your Model will be used to store the progress of the import.
Start by adding the InteractsWithImporter trait and the Importable contract to the Model.
You will also need to define the methods in the Importable contract
<?php namespace App\Models; use Alfonsobries\LaravelSpreadsheetImporter\Traits\InteractsWithImporter; use Alfonsobries\LaravelSpreadsheetImporter\Contracts\Importable; use Illuminate\Database\Eloquent\Model; class MyModel extends Model implements Importable { use InteractsWithImporter; /** * Return the full path of the file that will be imported * @return string */ public function getFileToImportPath() { // Notice that this line should be adapted to your application, this is an example for // a path that comes from a file that was stored using the spatie media library package return $this->getFirstMedia('file')->getPath(); } /** * Return the temporaly table name that will be used to store the spreadsheet contents * * @return string */ public function getTemporalTableName() { // This is an example you should adapt this line to your own application // You should create a method that always return the same value for the same model return sprintf('file_%s', $this->id); }
Add the neccesary columns to the Importable Model
Your Model will need a few columns to store the progress and status of the import, to add those columns you can create a migration like the following (just change the table name for your model table name):
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddImportableColumnsToModel extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('my_model_table', function (Blueprint $table) { $table->string('importable_process_id')->nullable(); $table->string('importable_table_name')->nullable(); $table->string('importable_total_rows')->nullable(); $table->string('importable_processed')->nullable(); $table->string('importable_status')->default('new'); $table->mediumText('importable_output')->nullable(); $table->mediumText('importable_feedback')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('my_model_table', function (Blueprint $table) { $table->dropColumn('importable_process_id'); $table->dropColumn('importable_table_name'); $table->dropColumn('importable_total_rows'); $table->dropColumn('importable_processed'); $table->dropColumn('importable_status'); $table->dropColumn('importable_output'); $table->dropColumn('importable_feedback'); }); } }
Once the Model is configured and you import a spreadsheet you can interact with the data stored in the temporal table using the tempData() relationship.
For example lets say that your spreadsheet has a sales column and you want the total:
$totalSales = $myModel->tempData()->sum('sales');
Import a file
For importing a file you will need to call the xlsx-laravel-spreadsheet-importer command using the instructions here but to make your work easy this package includes a StartImport Job that will build the command for you (dont forget to add the neccesary columns to store the progres to your model).
You can dispatch the job at any moment, it receive the importable Model as a param.
For example, you can call the job once you save your model in a store method in a controller (assuming that your model is associating a file and will return a valid file_path in the getFileToImportPath() method):
public function store(Request $request, MyModel $model) { $model->addFile($request->file); \Alfonsobries\LaravelSpreadsheetImporter\Jobs\StartImport::dispatch($model); // ...The rest of the code }
That job will create and trigger the Node command, then the Node command will call the ReportImporterProgress artisan command every time has some progress to inform, finally the artisan command will trigger a ImporterProgressEvent that you can listen with your custom event listener or by adding ImporterProgressEventListener that comes in this package, that listener is responsible to store the progress of your import into your Model:
To configure the event listener add the following lines into your app/Providers/EventServiceProvider.php:
class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ // Every time the importer has some progress \Alfonsobries\LaravelSpreadsheetImporter\Events\ImporterProgressEvent => [ // (Or use your own event listener) \Alfonsobries\LaravelSpreadsheetImporter\Listeners\ImporterProgressEventListener::class, ], // The `ImporterProgressEventListener` also creates the following observable events: // When the import finished \Alfonsobries\LaravelSpreadsheetImporter\Events\ImporterProgressFinishedEvent => [ // In this case you will need to define your own event listener ], // When the import reports an error: \Alfonsobries\LaravelSpreadsheetImporter\Events\ImporterProgressErrorEvent => [ // In this case you will need to define your own event listener ], ]; // ...
Testing
For running the tests you will need to install the dependencies and have a valid testing database
- Install the composer packages
composer install - Because this package interacts with a database you will need to define a
.envwith a valid mysql or postgresql database data (useenv.exampleas an example) - Also you need to install the npm dependency
npm install
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email alfonso@vexilo.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
alfonsobries/laravel-spreadsheet-importer 适用场景与选型建议
alfonsobries/laravel-spreadsheet-importer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 769 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xlsx」 「laravel」 「laravel-spreadsheet-importer」 「sheetjs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alfonsobries/laravel-spreadsheet-importer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alfonsobries/laravel-spreadsheet-importer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alfonsobries/laravel-spreadsheet-importer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Alfabank REST API integration
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Fast Excel import/export for Laravel
统计信息
- 总下载量: 769
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-01