如何修复 Laravel Excel UTF-8 字符
How to fix LaravelExcel UTF-8 Characters
我正在使用 Laravel 5.8,我想使用 LaravelExcel 从视图中下载数据。这是我的导出 Class:
namespace App\Exports;
use App\Member\Student;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Facades\Excel;
class StudentExportView implements FromView
{
/**
* @return \Illuminate\Support\Collection
*/
public function view(): View
{
return view('admin.students.custom', [
'students' => Student::take(1)->get(),
'customs' => Student::take(1)->get(),
]);
}
}
这里是下载的控制器方法csv
:
public function export_view()
{
return Excel::download(new StudentExportView, 'studentlist.csv');
}
这里是调用这个方法的路由:
Route::get('export_view','StudentAdminController@export_view')->name('students.export_view');
但问题是,下载的内容是这样的:
如您所见,标题已损坏,body 似乎是空的。
那么如何解决这个问题呢?
查看:
<table class="table table-sm table-bordered">
<thead>
<tr class="thead-dark">
<th>Row</th>
<th>Name</th>
<th>Family Name</th>
<th>National Code</th>
<th>Mobile Number</th>
<th>Province</th>
<th>City</th>
<th>Degree</th>
<th>Grade</th>
<th>Registered Time</th>
</tr>
</thead>
<tbody>
@if($students->count() != 0)
@foreach($students as $student)
<tr data-id="{{ $student->mbr_id }}"
data-mobile="{{ $student->mbr_mobile }}"
data-post-code="{{ $student->mbr_post_code }}"
data-address="{{ $student->mbr_address }}"
>
<td>{{ $student->mbr_name }}</td>
<td>
{{ $student->mbr_family }}
<a class="information text-danger float-left"><i class="fa fa-info-circle"></i></a>
</td>
<td>{{ $student->mbr_national_code }}</td>
<td>{{ $student->mbr_mobile }}</td>
<td>{{ $student->province }}</td>
<td>{{ $student->city }}</td>
<td>{{ $student->degree }}</td>
<td>{{ $student->grade }}</td>
<td>
@if($customs->find($student->mbr_id))
{{ jdate($customs->find($student->mbr_id)->created_at) }}
@endif
</td>
</tr>
@endforeach
@else
<tr>
<td colspan="12" style="text-align: center">No data for showing
</td>
</tr>
@endif
</tbody>
</table>
尝试使用此命令发布 excel 配置:-
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
然后,在 config/excel.php 中,找到并更改此键:
'use_bom' => false, to be 'use_bom' => true,
这解决了我的问题。
我正在使用 Laravel 5.8,我想使用 LaravelExcel 从视图中下载数据。这是我的导出 Class:
namespace App\Exports;
use App\Member\Student;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Facades\Excel;
class StudentExportView implements FromView
{
/**
* @return \Illuminate\Support\Collection
*/
public function view(): View
{
return view('admin.students.custom', [
'students' => Student::take(1)->get(),
'customs' => Student::take(1)->get(),
]);
}
}
这里是下载的控制器方法csv
:
public function export_view()
{
return Excel::download(new StudentExportView, 'studentlist.csv');
}
这里是调用这个方法的路由:
Route::get('export_view','StudentAdminController@export_view')->name('students.export_view');
但问题是,下载的内容是这样的:
如您所见,标题已损坏,body 似乎是空的。
那么如何解决这个问题呢?
查看:
<table class="table table-sm table-bordered">
<thead>
<tr class="thead-dark">
<th>Row</th>
<th>Name</th>
<th>Family Name</th>
<th>National Code</th>
<th>Mobile Number</th>
<th>Province</th>
<th>City</th>
<th>Degree</th>
<th>Grade</th>
<th>Registered Time</th>
</tr>
</thead>
<tbody>
@if($students->count() != 0)
@foreach($students as $student)
<tr data-id="{{ $student->mbr_id }}"
data-mobile="{{ $student->mbr_mobile }}"
data-post-code="{{ $student->mbr_post_code }}"
data-address="{{ $student->mbr_address }}"
>
<td>{{ $student->mbr_name }}</td>
<td>
{{ $student->mbr_family }}
<a class="information text-danger float-left"><i class="fa fa-info-circle"></i></a>
</td>
<td>{{ $student->mbr_national_code }}</td>
<td>{{ $student->mbr_mobile }}</td>
<td>{{ $student->province }}</td>
<td>{{ $student->city }}</td>
<td>{{ $student->degree }}</td>
<td>{{ $student->grade }}</td>
<td>
@if($customs->find($student->mbr_id))
{{ jdate($customs->find($student->mbr_id)->created_at) }}
@endif
</td>
</tr>
@endforeach
@else
<tr>
<td colspan="12" style="text-align: center">No data for showing
</td>
</tr>
@endif
</tbody>
</table>
尝试使用此命令发布 excel 配置:-
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
然后,在 config/excel.php 中,找到并更改此键:
'use_bom' => false, to be 'use_bom' => true,
这解决了我的问题。