An extra column and row appears in the exported excel file using Laravel maatwebsite/excel package

时间:2016-10-20 12:41:20

标签: php laravel maatwebsite-excel

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 个答案:

答案 0 :(得分:1)

当您尝试为整行应用某种样式时,我猜这是一个错误在PHPExcel 中出现。您可以通过将样式从行移动到单个单元格来解决它。 在这里,您在刀片模板中的初始“样式”会发生什么:

<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>

它只是为“奇数”和“偶数”行中的单元格定义样式。 现在你应该在你的记录中添加“odd”和“even”类:

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

这样,PHPExcel将直接将样式应用于单元格,并且不会添加其他单元格。