CustomExporter.php 4.3 KB
<?php
/**
 * +-----------------------------------------------------------------------------------------------------------------------
 * 扩展类:通用导出扩展类
 * +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Extensions
 * @package   App\Admin\Extensions
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年8月30日10:57:09
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Extensions;

use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Illuminate\Support\Arr;

/**
 * Class CustomExporter
 *
 * @category  App\Admin\Extensions
 * @package   App\Admin\Extensions
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年8月30日10:57:09
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class CustomExporter extends ExcelExporter
{
//    protected $filename = 'table';
//    protected $head = [];
//    protected $body = [];
//
//    public function __construct($filename = 'table', $head = null, $body = null, Grid $grid = null)
//    {
//        $this->filename = $filename;
//        $this->head = $head;
//        $this->body = $body;
//        parent::__construct($grid);
//    }
//
//    /**
//     * {@inheritdoc}
//     */
//    public function export()
//    {
//        $titles = [];
//        $filename = $this->filename.'.xls';
//        $data = $this->getData();
//        if (!empty($data)) {
//            $columns = Arr::dot($this->sanitize($data[0]));
//            $titles = array_keys($columns);
//        }
//        $output = self::putcsv(($this->head == []) ? array_keys($columns) : $this->head);
//
//        if ($this->body == []) {
//            foreach ($data as $row) {
//                $row = Arr::only($row, $titles);
//
//                $output .= self::putcsv(Arr::dot($row));
//                dump($output);
//            }
//        } else {
//            foreach ($this->body as $row) {
//                $output .= self::putcsv(Arr::dot($row));
//            }
//        }
//        dd(1);
//        $headers = [
//            'Content-Encoding'    => 'UTF-8',
//            'Content-Type'        => 'text/csv;charset=UTF-8',
//            'Content-Disposition' => "attachment; filename=\"$filename\"",
//        ];
//        response(rtrim($output, "\n"), 200, $headers)->send();
//        exit;
//    }
//
//    /**
//     * Remove indexed array.
//     *
//     * @param array $row
//     *
//     * @return array
//     */
//    protected function sanitize(array $row)
//    {
//        return collect($row)->reject(function ($val) {
//            return is_array($val) && !Arr::isAssoc($val);
//        })->toArray();
//    }
//
//    /**
//     * @param $row
//     * @param string $fd
//     * @param string $quot
//     *
//     * @return string
//     */
//    protected static function putcsv($row, $fd = ',', $quot = '"')
//    {
//        $str = '';
//        foreach ($row as $cell) {
//            $cell = str_replace([$quot, "\n"], [$quot.$quot, ''], $cell);
//            if (strstr($cell, $fd) !== false || strstr($cell, $quot) !== false) {
//                $str .= $quot.$cell.$quot.$fd;
//            } else {
//                $str .= $cell.$fd;
//            }
//        }
//        return substr($str, 0, -1)."\n";
//    }


    protected $fileName = '列表';
    protected $columns  = [];
    protected $index    = 0;

    /**
     * OrderExporter constructor.
     *
     * @param string $fileName
     * @param array $columns
     */
    public function __construct($fileName = '', $columns = [])
    {
        if ($fileName) {
            $this->fileName    = $fileName;
        }
//        $this->fileName .= '.csv';
        $this->fileName .= '.xls';

        if ($columns) {
            $this->columns    = $columns;
        }
//        $this->columns['id'] = '';
    }

    /**
     * @return array
     */
    public function headings(): array
    {
//        dump($this->columns);
//        dump(array_values($this->columns));
        if (!empty($this->columns)) {
            return array_values($this->columns);
        }

        return $this->headings;
    }
}