laravel 无法加载文件,无法加载为DOM文档

plicqrtu  于 2022-11-26  发布在  其他
关注(0)|答案(5)|浏览(188)

当我尝试将数据导出为Excel文件的特定请求,我得到了下面的错误
无法加载文档为DOM文档,请尝试以下操作:
我搜索了很多,但找不到任何答案,问题只出现在semester = First Sem时,但当学期是第二,第三等这个错误不来,我可以下载excel。
下面是我的代码

Route::get('/export', function () {
  return Excel::download(new ReportExport(request()->exams, request()->course, request()->semesterName), ''.request()->course.' '.request()->semesterName.'('.request()->exams.').xlsx');
})

// ReportExcel.php
$data = Fee::with(['backpapers' => function($bp) use ($myCourse, $semName) {
            $bp->where('course_name', $myCourse)->where('semester', $semName);
        }])->get();

 $table = '<table class="table table-border table-hover"><thead><tr><th>#</th><th>Roll No</th><th>Name</th><th>Reg_No</th><th>Father\'s Name</th><th>Gender</th><th>Aadhaar No</th><th>Mobile</th><th>Email</th><th>Paper 1</th><th>Paper 2</th><th>Paper 3</th><th>Paper 4</th><th>Exam Fee</th></tr></thead><tbody>';

 $count = 1;
 foreach ($data as $key => $bp) {
  if (count($bp->backpapers) > 0) {
    $table .= '<tr><td>'.$count.'</td><td>'.$bp->roll_number.'</td><td>'.$bp->name.'</td><td>'.($bp->registration_number != null ? $bp->registration_number : '-').'</td><td>'.$bp->father_name.'</td><td>'.($bp->gender == 1 ? 'Male' : 'Female').'</td><td>'.($bp->aadhaar_number != null ? $bp->aadhaar_number : '-').'</td><td>'.$bp->mobile_number.'</td><td>'.($bp->email != null ? $bp->email : '-').'</td>';
    
    $paperFee = BackpaperFee::where('number_of_paper', count($bp->backpapers))->pluck('fee_amount')->first();
    if (count($bp->backpapers) == 4) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
    } elseif (count($bp->backpapers) == 3) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td>';
    } elseif (count($bp->backpapers) == 2) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td><td>-</td>';
    } elseif (count($bp->backpapers) == 1) {
        foreach ($bp->backpapers as $bpp) {
            $table .= '<td>'.$bpp->paper_name.'</td>';
        }
        $table .= '<td>-</td><td>-</td><td>-</td>';
    }

    $table .= '<td>'.$paperFee.'</td></tr>';
    $count++;
  }
 }
 $table .= '</tbody></table>';
 
 return view('excel', compact('table'));

就像我前面说的,错误只在semester=First Sem时发生。
我试探着:

  1. php artisan cache:clear
  2. php artisan route:clear
  3. php artisan view:clear
  4. php artisan config:clear
    但是什么都不管用...提前感谢
koaltpgm

koaltpgm1#

从html元素中移除不需要的属性。在我的例子中,我移除了

<table id="employee" cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">

并添加了<table>,修复了该问题

l7mqbcuq

l7mqbcuq2#

问题是有一个关闭锚标记,这是造成问题的原因,因为在生成Excel时,不得有额外的东西或缺失,只有纯HTML表适用于Excel文件生成,没有其他内容

5lhxktic

5lhxktic3#

另外!检查你的文本中是否有“〉”和“〈”这样的符号,并将它们改为实体名称:

&gt;
&lt;
cetgtptt

cetgtptt4#

我的是我有一个多余的。
删除它,现在它的工作了

6xfqseft

6xfqseft5#

对我来说,发生错误是因为&
首先,它是<td><b>Created & Published </b></td>,因此会产生错误。
因此,我将其更改为<td><b>Created and Published </b></td>,问题已解决。
如果循环中的标签或值中包含“&“,则可能会导致错误

相关问题