Reputation:
I'm using Laravel 5.8 and I want to download data from view with LaravelExcel. Here is my Export 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(),
]);
}
}
And here is the Controller Method for downloading csv
:
public function export_view()
{
return Excel::download(new StudentExportView, 'studentlist.csv');
}
And here is the route that calls this method:
Route::get('export_view','StudentAdminController@export_view')->name('students.export_view');
But the problem is, the content that gets downloaded goes like this:
As you can see the headings are broken and body seems to be empty.
So how to solve this issue?
View:
<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>
Upvotes: 0
Views: 5103
Reputation: 830
try to publish the excel config by using this command :-
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
then , in config/excel.php , find and change this key :
'use_bom' => false, to be 'use_bom' => true,
this solve the problem for me.
Upvotes: 3