2
votes

I recently downloaded maatwebsite/excel package in Laravel for exporting the database records in Excel format.

The installation was successful and I can export the expected database records in excel (.xls) format.

But I have notice that there is an extra column and row being inserted to the expected data as shown below:

enter image description here

Here row no. 8 and column H are unwanted. Following is my controller code:

...
$audit_logs = $model->whereBetween('audit_log_created_date', [$_from, $_to])->get();
$file_name  = 'Audit Log Report ('.$_from.' to '.$_to.')';
$type       = AuditLog::$audit_log_type;
$action     = AuditLog::$audit_log_action;

Excel::create($file_name, function($excel) use ($audit_logs,$type,$action) {
    $excel->sheet('Sheet 1', function($sheet) use ($audit_logs,$type,$action) {
        $sheet->loadView('/admin/audit-log/_export')->with([
                    'audit_logs' => $audit_logs,
                    'type'       => $type,
                    'action'     => $action
                ]);
    });
})->export();

And following is my view code:

<style>
    .trr {
        background-color: #0099FF;
        color: #ffffff;
        align: center;
        padding: 10px;
        height: 20px;
    }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<table>
    <thead>
    <tr class="trr">
      <td>S.no.</td>
      <td>Type</td> 
      <td>Action</td>
      <td>Event</td>
      <td>Description</td>
      <td>Username</td>
      <td>Date & Time</td>
    </tr>
    </thead>
    <tbody>
    @if(!empty($audit_logs))
    <?php $count = 0; ?>
    @foreach($audit_logs as $audit_log)
    <?php $count = $count+1; ?>
    <tr style="border: 1px solid #040404; background-color: <?php if($count%2==0) echo '#E3F2FD'; else echo '#BBDEFB'; ?>;">
      <td>{{ $count }}</td>
      <td>{{ $type[$audit_log->audit_log_type] or null }}</td>
      <td>{{ $action[$audit_log->audit_log_action] or null }}</td>
      <td>{{ $audit_log->audit_log_event_id or null}}</td>
      <td>{{ $audit_log->audit_log_description or null}}</td>
      <td>{{ $audit_log->user->user_name or null}}</td>
      <td>{{ $audit_log->audit_log_created_date or null}}</td>
    </tr>
    @endforeach
    @endif
    </tbody>
</table>
</html>

If somebody can identify the issue please let me know.

Thank you for the help.

1

1 Answers

1
votes

I guess it's a bug that occurs in PHPExcel when you try to apply some style for the entire row. You can solve it by moving styles from row to single cell. Here what happens with your initial "style" in the blade template:

<style>
    .trr {
        background-color: #0099FF;
        color: #ffffff;
        align: center;
        padding: 10px;
        height: 20px;
    } 
    td {
        border: 1px solid #040404;
    }
    tr.odd > td {
        background-color: #E3F2FD;
    }

    tr.even > td {
        background-color: #BBDEFB;
    }
</style>

It simply defines style for the cells in the "odd" and "even" rows. Now you should add "odd" and "even" class to your record:

<tr class="{{count % 2 == 0? "even": "odd"}}">

In this way PHPExcel will apply style directly to the cells and won't add additional cell.