xiaomlove / grid-exporter
export grid data for laravel-admin
Installs: 6 060
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 4
Open Issues: 0
Requires
- php: >=7.0.0
- encore/laravel-admin: ~1.6
- maatwebsite/excel: ^3.1
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2025-03-28 03:19:45 UTC
README
Installation
$ composer require xiaomlove/grid-exporter -vvv
Configuration
grid-exporter
supports 1 configuration, open config/admin.php
find extensions
:
'extensions' => [ 'grid-exporter' => [ // Set this to false if you want to disable this extension 'enable' => true, ] ]
Usage
if this extension is enabled, it will register as the default exporter (replace Encore\Admin\Grid\Exporters\CsvExporter
)
if you want to format specified column, use it manually. In your controller
use Chenyulingxi\LaravelAdmin\GridExporter\Exporter; use Maatwebsite\Excel\Events\AfterSheet; use Maatwebsite\Excel\Events\BeforeExport; ... protected function grid() { $grid = new Grid(new Test); $grid->id('Id'); $grid->name('Name'); $grid->created_at('Created at'); $grid->updated_at('Updated at'); $exporter = new Exporter(); //format the name column $exporter->format('name', function ($value) { //In the format callback closure, $this bindTo the eloquent model return strtolower($value); }); // replace the grid table header $exporter->withHeadings([ 'id' => '编号', 'name' => '姓名', 'created_at' => '创建时间', 'updated_at' => '更新时间', ]); //change output file (xlsx) style $exporter->setEvents([ BeforeExport::class => function(BeforeExport $event) { $event->writer->getDelegate()->getProperties()->setCreator('xiaomlove'); }, AfterSheet::class => function ($event) { $sheet = $event->sheet; $highestColumn = $sheet->getHighestColumn(); $highestRow = $sheet->getHighestRow(); $styles = [ 'font' => [ 'bold' => true, ], 'fill' => [ 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR, 'rotation' => 90, 'startColor' => [ 'argb' => 'FFA0A0A0', ], 'endColor' => [ 'argb' => 'FFFFFFFF', ], ] ]; $sheet->getStyle("A{$highestRow}:{$highestColumn}{$highestRow}")->applyFromArray($styles); } ]); // set write type, default xlsx $exporter->setWriteType(\Maatwebsite\Excel\Excel::CSV); // set the file name $exporter->setFileName('test-export'); $grid->exporter($exporter); return $grid; } ...
if you want more control over the output file, you can create a class that extents from Chenyulingxi\LaravelAdmin\GridExporter\DataSource
, then inject it's instance to the exporter like this:
$exporter->setDataSource(new TestDataSource());
more information reference to Laravel Excel and PhpSpreadsheet
License
Licensed under The MIT License (MIT).