当我尝试根据特定请求将数据导出为 Excel 文件时,出现以下错误
PhpOffice\PhpSpreadsheet\Reader\异常 无法将 path_to_project\storage ramework\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
时才会发生错误。
我尝试过:
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:clear
但是没有任何作用..提前感谢
从 html 元素中删除不需要的属性。 就我而言,我删除了
<table id="employee" cellpadding="0" cellspacing="0" border="0" class="display table table-bordered" id="hidden-table-info">
并添加
<table>
解决了问题
还有! 检查文本中是否有“>”和“<" and change them with entities names:
”等符号>
<
对我来说,发生错误是因为
&
首先,它是
<td><b>Created & Published </b></td>
,所以它产生了错误。
所以我把它改成
<td><b>Created and Published </b></td>
,就解决了。
如果标签或循环中的值中有“
&
”,可能会导致错误
问题是有一个结束锚标记,这导致了问题,因为在生成 Excel 时不能有额外的东西或缺少仅纯 HTML 表适用于 excel 文件生成没有其他
我的是我有一个额外的。
删除它,现在又可以工作了
我的视图中有一个按钮标签,它是用于导出的参考视图文件。
我所做的是,我创建了另一个视图文件,然后从第一个具有按钮的视图文件复制了 html 格式。然后我删除了按钮标签。
然后现在就可以正常工作了。