Search code examples
phplaravellaravel-5.6maatwebsite-excellaravel-excel

PHP / Laravel - How can I display table header on each page print?


I'm using Laravel 5.6 with maatwebsite(laravel-excel) 3.1 And my php verson is 7.1

Here is the ReportExport class :

<?php
namespace App\Exports;
use App\Report;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
class ReportExport implements ShouldAutoSize, FromView, WithColumnFormatting, WithEvents
{
    use Exportable;
    protected $id;
    public function __construct($id)
    {
        $this->id = $id;
    }
    public function view(): View
    {
        $report = Report::find($this->id);
        return view('exports.report', [
            'report' => $report,
        ]);
    }
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->getDelegate()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
                ...
            },
        ];
    }
}

My view blade export laravel like this :

<table>
    ...
    <thead>
    <tr>
        <th colspan="2" rowspan="2">No</th>
        <th colspan="7" rowspan="2">Mata Pelajaran</th>
        <th colspan="17">Pengetahuan</th>
        <th colspan="17">Keterampilan</th>
    </tr>
    <tr>
        <th colspan="2">KB</th>
        <th colspan="3">Angka</th>
        <th colspan="3">Predikat</th>
        <th colspan="9">Deskripsi</th>
        <th colspan="2">KB</th>
        <th colspan="3">Angka</th>
        <th colspan="3">Predikat</th>
        <th colspan="9">Deskripsi</th>
    </tr>
    </thead>
    <tbody>
    ...
    @php ($group = 'A')
    @php ($number = 0)
    @foreach($values as $item)
    @if($number==0 || $group!=$item['group'])
    <tr>
        <td colspan="9">Kelompok {{$item['group']}}</td>
        <td colspan="2"></td>
        <td colspan="3"></td>
        <td colspan="3"></td>
        <td colspan="9"></td>
        <td colspan="2"></td>
        <td colspan="3"></td>
        <td colspan="3"></td>
        <td colspan="9"></td>
    </tr>
    @php ($number = 0)
    @endif
    <tr>
        <td colspan="2">{{++$number}}</td>
        <td colspan="7">{{$item['lesson_name']}}</td>
        <td colspan="2">{{$item['kb_pengetahuan']}}</td>
        <td colspan="3">{{$item['nilai_pengetahuan']}}</td>
        <td colspan="3">{{$item['predikat_pengetahuan']}}</td>
        <td colspan="9">{{$item['deskripsi_pengetahuan']}}</td>
        <td colspan="2">{{$item['kb_keterampilan']}}</td>
        <td colspan="3">{{$item['nilai_keterampilan']}}</td>
        <td colspan="3">{{$item['predikat_keterampilan']}}</td>
        <td colspan="9">{{$item['deskripsi_keterampilan']}}</td>
    </tr>
    @php ($group = $item['group'])
    @endforeach
    </tbody>
</table>

The script is works. But If I see on the print preview, the header of table only display in the page 1. I want the table header to appear on all pages

How can I do it?


Solution

  • As you are using blade here.. so you need to repeat the header based on condition to separate for each page. As how much records you would like to display on page.

    Better to create this as sub-view and include the header based on the condition.. I mean after the no of rows (based on the space adjusted on the page).

    Here is the documentation to use subviews - https://laravel.com/docs/5.8/blade#including-sub-views

    If you are using to generate the excel from record you can use this approach.

    Set rows to repeat at top when printing - PHPExcel

    Let me know if it helps.