PhpOffice\PhpSpreadsheet\Reader\Exception 无法加载....作为 DOM 文档

PhpOffice\PhpSpreadsheet\Reader\Exception Failed to load .... as a DOM document

提问人:Vpa 提问时间:11/19/2020 更新时间:6/7/2023 访问量:22964

问:

当我尝试在特定请求上将数据导出为excel文件时,我得到了以下错误

PhpOffice\PhpSpreadsheet\Reader\Exception 无法将 path_to_project\storage\framework\laravel-excel\laravel-excel-pz23rYwVENIOZUw7vnvhKaNjUkkNLNT8.html 加载为 DOM 文档

我搜索了很多,但找不到任何答案,问题只在学期是第二、第三等时才出现,这个错误没有出现,我可以下载 excel。semester = First Sem

以下是我的代码

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

但没有任何效果..提前致谢

Laravel phpSpreadsheet laravel-Excel

评论


答:

6赞 Asheeka K P 12/4/2020 #1

从 html 元素中删除不需要的属性。 就我而言,我删除了

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

并添加了解决了该问题<table>

1赞 Duc Tong 5/21/2021 #2

问题是有一个关闭锚标记,这是造成问题的,因为在生成 Excel 时,不能有额外的东西或缺少只有纯 HTML 表格适用于 excel 文件生成,没有其他任何东西

3赞 fireball70 9/1/2021 #3

也! 检查文本中是否有“>”和“<”等符号,并使用实体名称更改它们:

&gt;
&lt;

评论

2赞 topher 9/27/2022
对我来说也是“&”
0赞 Apit John Ismail 10/4/2022 #4

我的是我有一个额外的.

删除它,现在它又开始工作了

2赞 Yasin Patel 11/23/2022 #5

对我来说,发生错误是因为&

首先,它产生了一个错误。<td><b>Created & Published </b></td>

所以我把它改成了,它解决了。<td><b>Created and Published </b></td>

如果标签中有“”或循环中的值,则可能会导致错误&

评论

0赞 Raphael Cunha 1/27/2023
您可以将 & 替换为 以仍然显示 & 符号&amp;