1. Go to this page and download the library: Download xiaomlove/grid-exporter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xiaomlove / grid-exporter example snippets
'extensions' => [
'grid-exporter' => [
// Set this to false if you want to disable this extension
'enable' => true,
]
]
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;
}
...
$exporter->setDataSource(new TestDataSource());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.