温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Laravel Excel3.0导出的方法

发布时间:2020-12-10 10:30:11 来源:亿速云 阅读:168 作者:小新 栏目:编程语言

这篇文章主要介绍了Laravel Excel3.0导出的方法,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

                                                          

导出方法抽离:

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class Export extends StringValueBinder implements FromCollection,
    ShouldAutoSize,WithColumnFormatting,WithCustomValueBinder,WithStrictNullComparison,WithEvents
{
    private $row;
    private $data;
    private $mergeCell;
    private $columnName;
    private $formatNumber;

    /*
     * $mergeCell $columnName :合并单元格所需参数;
     * $mergeCell 需要合并的位置数组以MAP形式存储 [开始行=>结束行]
     * $columnName 需要合并列 与合并行数结合使用ARRAY存储 ['A','B']
     */
    public function __construct($row,$data,$mergeCell=null,$columnName=null,$formatNumber=[])
    {
        $this->row = $row;
        $this->data = $data;
        $this->mergeCell = $mergeCell;
        $this->columnName = $columnName;
        $this->formatNumber = $formatNumber;
    }

    public function collection()
    {
        $row = $this->row;
        $data = $this->data;

//设置表头
        foreach ($row[0] as $key => $value) {
            $key_arr[] = $key;
        }

//输入数据
        foreach ($data as $key => &$value) {
            $js = [];
            for ($i=0; $i < count($key_arr); $i++) {
                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
            }
            array_push($row, $js);
            unset($val);
        }
        return collect($row);
    }
    public function registerEvents(): array
    {
        // TODO: Implement registerEvents() method.
        if ($this->mergeCell && $this->columnName){
            return [
                AfterSheet::class => function(AfterSheet $event){
                    foreach ($this->columnName as $column){
                        foreach ($this->mergeCell as $key=>$value){
                            $event->sheet->getDelegate()->mergeCells($column.$key.':'.$column.$value);
                        }
                    }
                }
            ];
        }
        return [];
    }

    public function columnFormats(): array{
        $formatNumber = [];
        foreach ($this->formatNumber as $column){
            $formatNumber[$column] = NumberFormat::FORMAT_TEXT;
        }
        return $formatNumber;
    }
}

使用:

/*表头表体都为二维数组*/
$row=[['row1'=>'列1','row2'=>'列2']];
/*与表头key对应,缺少数据报错*/
$list=[['row1'=>'行1列1','row2'=>'行1列2'],['row1'=>'行2列1','row2'=>'行2列2']];
/*将第一行到第三行,第五行到第七行的A,B,C列各自合并*/
$mergeCell=[1=>3,5=>7];
$columnName=["A","B","C"];
/*数字过长的列转换格式防止科学计数*/
$formatNumber=['A','B','C'];
//上方A,B,C列都为示意,根据自己需求调整,对应EXCEL的列
return Excel::download(new Export($row,$list,$mergeCell,$columnName,$formatNumber),'fileName');

感谢你能够认真阅读完这篇文章,希望小编分享Laravel Excel3.0导出的方法内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI