>  기사  >  PHP 프레임워크  >  Laravel Excel3.0에서 내보내는 방법

Laravel Excel3.0에서 내보내는 방법

藏色散人
藏色散人앞으로
2020-09-18 09:23:442874검색

튜토리얼 칼럼에서 Laravel Excel 3.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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 learnku.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제