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

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64