Laravel-admin 自定义csv导出,支持原有导出csv的所有功能,导出所有数据使用分页查询处理

新建基础导出类

<?php


namespace App\Admin\Extensions\Exports;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Database\Eloquent\Model;

class BaseExport extends AbstractExporter
{
    public function __construct(Grid $grid = null)
    {
        parent::__construct($grid);
    }

    /**
     * @var string
     */
    protected $fileName;

    /**
     * @var array
     */
    protected $headings = [];

    /**
     * @var array
     */
    protected $columns = ['*'];

    /**
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     */
    public function query()
    {
        if (!empty($this->columns)) {
            return $this->getQuery()->select($this->columns);
        }

        return $this->getQuery();
    }

    public function export()
    {
        $this->csv($this->fileName,$this->headings,$this->query());
    }

    public function map(Model $model){
        return [];
    }

    /**
     * 导出csv
     *
     * @param String $sFileName
     * @param array $aTitle
     * @param $oQuery
     * @author zjh
     */
    protected function csv($sFileName,Array $aTitle, $oQuery)
    {
        // 设置过期时间
        set_time_limit(0);
        //处理需要导出的数据
        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'. $sFileName .'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $fp = fopen('php://output', 'a');//打开output流
        fwrite($fp, "\xEF\xBB\xBF"); // 写入bom 头 可识别 utf8
        fputcsv($fp, $aTitle);//将数据格式化为CSV格式并写入到output流中

        $perSize = 10000;//每次查询的条数

        $oQuery->chunkById($perSize,function ($collection) use($fp){
            foreach($collection as $obj) {
                $rowData = $this->map($obj); //返回 array
                fputcsv($fp, $rowData);
            }
            unset($oCollection);//释放变量的内存
            //刷新输出缓冲到浏览器
            if (ob_get_level() > 0) {
                ob_flush();
            }
        });

        fclose($fp);
        exit();
    }


}

创建导出类

<?php
namespace App\Admin\Extensions\Exports;

use Illuminate\Database\Eloquent\Model;

class UserExport extends BaseExport
{

    /**
     * 导出文件名
     * @var string
     */
    public $fileName = 'test.csv';

    /**
     * 导出标题
     * @var string[]
     */
    public $headings = [
        'ID','用户名','创建时间','用户角色'
    ];

    /**
     * 导出查询字段 默认 *
     * @var string[]
     */
    public $columns = [
        'id', 'username', 'created_at', 'role_id'
    ];

    /**
     * 导出设置映射
     * @param Model $model
     * @return array
     */
    public function map(Model $model)
    {
        return [
            $model->id,
            $model->username,
            $model->created_at,
            data_get($model,'role.name','')
        ];
    }

在laravel-admin 控制器中 使用

  protected function grid()
    {
        $grid = new Grid(new Application);

         $grid->exporter(new UserExport());
    }

已发布成 composer summer-wind 包 ,欢迎下载使用

本作品采用《CC 协议》,转载必须注明作者和本文链接
喜欢的话就点个赞吧!
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!