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:
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.
答案 0 :(得分:1)
<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将直接将样式应用于单元格,并且不会添加其他单元格。