1、安装 larave-excel
composer require maatwebsite/excel
注意 laravel 版本号,目前支持 5.8-8 以及以上的版本,默认安装的是 3.1 版本。
<?php
namespace App\Admin\Extensions;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class Export extends AbstractExporter implements WithMapping, WithHeadings, FromCollection
{
use Exportable;
protected $titles = [];
public function __construct($fileName = null)
{
$this->fileName = !empty($fileName) ? $fileName : $this->getFilename();
$this->fileName .= '.xlsx'; //拼接下载文件名称
$this->titles = ['id' => 'id', 'user_id' => '所属用户' ,'created_at'=>'创建时间','updated_at'=>'更新时间'];
parent::__construct();
}
public function export()
{
// TODO: Implement export() method.
$this->download($this->fileName)->prepare(request())->send();
exit;
}
public function collection()
{
// TODO: Implement collection() method.
return collect($this->buildData());
}
public function headings(): array
{
// TODO: Implement headings() method.
return $this->titles();
}
public function map($row): array
{
// 直接返回row 导出全部字段 dd($row);
// return $row;
// TODO: Implement map() method.
return [
$row['id'],
$row['user_id'],
$row['created_at'],
$row['updated_at'],
];
}
}
可以看出,我们仅仅只需要关注 map
和 title
即可。
2、在控制器中使用
$grid->export(new Export('投产比'));
3、数据仓库
<?php
namespace App\Admin\Repositories;
use App\Models\ProductionRatio as Model;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Support\Facades\DB;
class ProductionRatio extends EloquentRepository
{
/**
* Model.
*
* @var string
*/
protected $eloquentClass = Model::class;
protected $translation;
public function get(Grid\Model $model)
{
$model->usePaginate(true);
$current_page = $model->getCurrentPage();
$per_page = $model->getPerPage();
$is_export = $model->filter()->input('_export_', null);
$date_start = $model->filter()->input('date.start', null);
$date_end = $model->filter()->input('date.end', null);
$where = [];
if ($date_start && $date_end) {
$where[] = ['at_date', '>=', date('Ymd', strtotime($date_start))];
$where[] = ['at_date', '<=', date('Ymd', strtotime($date_end))];
}
//处理分页显示的日期范围
if ($is_export == Grid\Exporter::SCOPE_ALL) {
$list = DB::connection('mysql')->table('production_ratio')
->where($where)
->orderByDesc('at_date')
->get()->map(function ($value) {
return (array)$value;
})->toArray();
foreach ($list as &$value) {
$value['rol'] = $value['cost'] > 0 ? number_float($value['profit'] / $value['cost']) : 0;
}
return $list;
} else {
$list = DB::connection('mysql')->table('production_ratio')
->where($where)
->orderByDesc('at_date')
->skip(($current_page - 1) * $per_page)
->take($per_page)
->get()->map(function ($value) {
return (array)$value;
})->toArray();
}
foreach ($list as &$value) {
$value['rol'] = $value['cost'] > 0 ? number_float($value['profit'] / $value['cost']) : 0;
}
$total = DB::connection('mysql')->table('production_ratio')
->where($where)
->count();
return $model->makePaginator(
$total,
$list
);
}
}