【问题标题】:PhpOffice\PhpSpreadsheet\Reader\Exception Failed to load .... as a DOM documentPhpOffice\PhpSpreadsheet\Reader\Exception 无法加载 .... 作为 DOM 文档
【发布时间】:2020-11-19 07:11:41
【问题描述】:

当我尝试根据特定请求将数据导出为 excel 文件时,出现以下错误

PhpOffice\PhpSpreadsheet\Reader\Exception 无法将 path_to_project\storage\framework\laravel-excel\laravel-excel-pz23rYwVENIOZUw7vnvhKaNjUkkNLNT8.html 作为 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

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

【问题讨论】:

    标签: laravel phpspreadsheet laravel-excel


    【解决方案1】:

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

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

    并添加了&lt;table&gt; 已解决问题

    【讨论】:

      【解决方案2】:

      问题是有一个关闭锚标记,这是造成问题的原因,因为在生成 Excel 时必须没有额外的东西或缺少纯 HTML 表格,仅适用于生成 excel 文件

      【讨论】:

        【解决方案3】:

        还有! 检查您的文本中是否有 ">" 和 "

        &gt;
        &lt;
        

        【讨论】:

          猜你喜欢
          • 2021-04-28
          • 2021-04-01
          • 2021-11-25
          • 2019-07-30
          • 2018-06-22
          • 2017-10-29
          • 2018-06-15
          • 2022-01-11
          相关资源
          最近更新 更多