登录  /  注册
首页 > php框架 > Laravel > 正文

Laravel Excel3.0如何导出

藏色散人
发布: 2020-09-18 09:23:44
转载
2639人浏览过

下面由Laravel教程栏目给大家介绍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存储 [&#39;A&#39;,&#39;B&#39;]
     */
    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.&#39;:&#39;.$column.$value);
                        }
                    }
                }
            ];
        }
        return [];
    }

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

使用:

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

以上就是Laravel Excel3.0如何导出的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号