Hossein Ahmadi
Hossein Ahmadi

Reputation: 849

UTF-8 encoding issue with laravel excel library

I used laravel excel library in my project to export blade files to excel files. my website language is persian with UTF-8 encoding. my problem is when user downloads the xls or csv file its have bad characters and if files saved as xlsx its show nothing. this is my blade file code is :

<ul style="display: flex;justify-content: space-between;align-items: center;max-width: 800px; background-color:#ff008f;margin: 0 auto;text-align: center; color:#fff;min-width:500px;">
 <li style="display: inline-block;padding:8px 10px;width: 35px;">ردیف</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">نام مشتری</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">شماره فاکتور</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">تعداد</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">تاریخ ثبت</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">ساعت ثبت</li>
 <li style="display: inline-block;padding:8px 10px;width: 200px;">مبلغ کل</li>
</ul>
<div>
<?php
  $i=1;
  foreach($fields as $field){    
?>
<ul style="display: flex;justify-content: space-between;align-items: center;max-width: 800px;margin: 0 auto;text-align: center; color:#111;min-width:500px;">
  <li style="display: inline-block;width: 35px;padding:8px 10px;"><?php echo $i?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $user->name?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $field->refid?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $count?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $jDate?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $field->time?></li>
  <li style="display: inline-block;width: 200px;padding:8px 10px;"><?php echo $field->total_fee?></li>
</ul>
<?php
$i++;
$array = [$fields,$user,$count];
 }

and my export to excel function code is :

public function exportXls(Request $request){
  $fromDate = $request->input('fromDate');
  $toDate=$request->input('toDate');
  $fromDate = explode('/',$fromDate);
  $fromGdate = jalali_to_gregorian($fromDate[2],$fromDate[1],$fromDate[0]);
  $toDate = explode('/',$toDate);
  $toGdate = jalali_to_gregorian($toDate[2],$toDate[1],$toDate[0]);
  $fromTime = $request->input('fromTime');
  $toTime = $request->input('toTime');
  $data['fields'] = DB::table('z_orders')->whereBetween('date',array($fromGdate,$toGdate))->whereBetween('time',array($fromTime,$toTime))->get();
  $data['without'] = false;
  Excel::create('excelFile', function($excel) use($data) {
    $excel->sheet('excelSheet', function($sheet) use($data) {
      $sheet->loadView('admin.c-filter',$data);
    });
  })->download('xls');
  return response()->json('YES');
 }

i used ajax request to call the exportXls function. and this is picture of what this library gaves to me: i have link in my blade file to download excel file that i gave that english name 'excel' that is correct here but others no.

Upvotes: 1

Views: 6893

Answers (2)

SlowSuperman
SlowSuperman

Reputation: 628

This works for me:

http://www.maatwebsite.nl/laravel-excel/docs/blade

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Upvotes: 0

Ievgen Kliushnyk
Ievgen Kliushnyk

Reputation: 109

add <meta charset="utf-8"> to your view or create a layout

<!DOCTYPE html>
<html lang="{{ \Lang::getLocale() }}">
<head>
    <meta charset="utf-8">
</head>
<body>
    @yield('content')
</body>
</html>

Upvotes: 1

Related Questions