douyan1903
2017-10-22 16:04
浏览 180

Laravel excel - 导出为PDF / XLS,显示希腊语/其他语言的乱码

I'm using Laravel 5.2 with maatwebsite/excel": "~2.1.0 package. Now when i'm trying to export to PDF / XLS data in English it's works fine. But when i'm trying to export data in other language i'm getting gibberish data.

Code:

 public function show(Request $request, $id)
{
    //var_dump($request->all());
    $type = $id;
    $date = ($request->input('date') == '0' ? false : $request->input('date'));
    $account = ($request->input('account') == '0'  || !$request->has('account') ? false : $request->input('account'));
    $device = ($request->input('device') == '0' ? false : $request->input('device'));
    $search = ($request->input('search') == false || count($request->input('search')) == 0 ? false : $request->input('search'));
    $client_id = $request->input('client_id');
    $data = $this->getConversionsByDate($date, $account, $device,$search,$client_id);
    $data = $this->parseData($data['conversions'],$type);
    //echo "<pre>".print_r($this->parseData($data['conversions']),true)."</pre>";
    return Excel::create('Poptin_conversions_data_'.Carbon::now()->toDateTimeString(), function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data){
            $sheet->fromArray($data);
        });
    })->download($type);
}

protected function parseData($data,$type){
    $conversions =[];

    if ($type=='xls'){
        foreach($data as $value){
            $conversions[] = [
                'created_at' =>$value->created_at,
                'account_name' =>$value->account_name,
                'poptin_name' =>$value->poptin_name,
                'poptin_domain' =>$value->poptin_domain,
                'visitor' =>trans('lang.'.$value->visitor),
                'name' =>$value->name,
                'phone' =>$value->phone,
                'email' =>$value->email,
                'message' =>$value->message,
                'other_fields' => str_replace(array('a:','b:'),'',$value->other_fields),
                'conversion_url' =>$value->conversion_url,
            ];
        }       
    }else if ($type=='pdf'){
        foreach($data as $value){
            $conversions[] = [
                'created_at' =>$value->created_at,
                'name' =>(empty($value->name) ? '#' : $value->name),
                'phone' =>(empty($value->phone) ? '#' : $value->phone),
                'email' =>(empty($value->email) ? '#' : $value->email),
                'message' =>(empty($value->message) ? '#' : $value->message),
                'other_fields' =>(empty($value->other_fields) ? '#' : str_replace(array('a:','b:'),'',$value->other_fields)),
            ];
        }   
    }

    return $conversions;
}

Look at the "Name" Cells

How it's look in blade view:

enter image description here

How it's look when export to PDF / XLS :

enter image description here

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐 更多相似问题