【问题标题】:An extra column and row appears in the exported excel file using Laravel maatwebsite/excel package使用 Laravel maatwebsite/excel 包导出的 excel 文件中出现额外的列和行
【发布时间】:2016-10-20 12:41:20
【问题描述】:

我最近在 Laravel 中下载了 maatwebsite/excel 包,用于以 Excel 格式导出数据库记录。

安装成功,我可以将预期的数据库记录导出为 excel (.xls) 格式。

但我注意到有一个额外的列和行被插入到预期的数据中,如下所示:

这里的行号。 8 和 H 列是不需要的。 以下是我的控制器代码:

...
$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();

以下是我的视图代码:

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

如果有人能找出问题,请告诉我。

感谢您的帮助。

【问题讨论】:

    标签: php laravel maatwebsite-excel


    【解决方案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>
    

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

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

    这样,PHPExcel 将直接将样式应用于单元格,而不会添加额外的单元格。

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 2020-09-04
      • 2017-03-07
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2019-05-15
      相关资源
      最近更新 更多