美文网首页
Laravel-admin自定义导出报表

Laravel-admin自定义导出报表

作者: bbdlg | 来源:发表于2019-03-30 21:08 被阅读0次

App\Admin\Extensions 增加 CustomExporter.php;

<?php

namespace App\Admin\Extensions;
use Encore\Admin\Grid;
use Illuminate\Support\Arr;
use Encore\Admin\Grid\Exporters\AbstractExporter;

class CustomExporter extends AbstractExporter
{
    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.'.csv';
        $data = $this->getData();
        if (!empty($data)) {
            $columns = array_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 = array_only($row, $titles);
                $output .= self::putcsv(array_dot($row));
            }
        }else {
            foreach ($this->body as $row) {
                $output .= self::putcsv(array_dot($row));
            }
        }
        $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";
    }
}

代码调用:

  //自定义报表导出格式
        $cards = Card::with('product', 'site')
            ->whereIn('state', [3,4])
            ->get() ;
        $head = ['序号', '卡号', '产品名称', '所属网点', '类别', '创建时间', '更新时间'];
        $body = [];
        foreach ($cards as $card) {
            $body[] = [
                $card['id'],
                $card['number'],
                $card['product']['name'],
                $card['site']['name'],
                $card['state']==3 ? '作废' : '退卡',
                $card['created_at'],
                $card['updated_at']
            ];
        }
        $grid->exporter(new CustomExporter('退废卡报表', $head, $body));

相关文章

  • Laravel-admin自定义导出报表

    在 App\Admin\Extensions 增加 CustomExporter.php; 代码调用:

  • laravel-admin 自定义导出excel功能,并导出图片

    最近用laravel-admin在做一个小项目,其中用到了excel导出功能。 但是laravel-admin自带...

  • 如何优雅的导出 Excel

    前言 公司项目最近有一个需要:报表导出。整个系统下来,起码超过一百张报表需要导出。这个时候如何优雅的实现报表导出,...

  • 拼多多自动发货

    拼多多商家后台-发货管理-订单查询-选择最近3个月订单- 选择批量导出- 自定义报表 字段选择: 商品 订单...

  • 工作中遇到的问题

    1.报表导出过慢的问题:在一次报表导出数据的过程中,随着业务量的增加,导出一个报表的时间过长,达到了2至3s钟,业...

  • Kendoui之grid保存为Excel

    在制作报表时常会遇到的需求是报表的导出。将报表导出为一个Excel格式更便于浏览、维护及打印。同时Grid也提供了...

  • 002.关于报表类需求分析

    1.报表导出字段 避免导出冗余字段 2.报表导出格式 一般为csv格式,xls格式有数量行的限制 但注意csv的格...

  • JAVA使用POI中XSSF方法导出excel

    今天分享的是POI方法导出excel,这两个月时间我的大部分工作都是导出报表,今天就给大家分享excel报表的导出...

  • 通达信数据转移

    导入导出自定义板块 顶栏-工具-自定义板块设置-导入板块 导入导出自定义公式 Ctrl+F-导出公式-技术指标公式...

  • Java ThreadLocal 妙用

    前言 最近公司需要做一个功能:导出报表的数据到 Excel 中,要求按报表内容分多个 sheet 导出;我用开源的...

网友评论

      本文标题:Laravel-admin自定义导出报表

      本文链接:https://www.haomeiwen.com/subject/svzsbqtx.html