2016-12-05 75 views
0

如何生成Microsoft Office Excel支持的CSV文件?Laravel创建支持Microsoft Office Excel的CSV

我用Laravel/Excel创建了CSV(http://www.maatwebsite.nl/laravel-excel/),内容是日文字符。

一切都很完美,直到我使用Microsoft Office Excel打开CSV文件时,日本文字不能被Microsoft Office Excel读取,我已经在我的视图上将文本编码为UTF-8(因为我使用loadView()函数创建了CSV)

我尝试通过记事本打开CSV文件并保存,没有任何更改,并使用Microsoft Office Excel再次打开并显示日文字符。

它发生了什么?我用记事本打开了这两个文件(默认的一个和记事本保存的csv),并检查不同的内容文件没有什么不同。

笔者认为:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/plain; charset=UTF-8" /> 
    </head> 
    <body> 
     <table> 
      <thead> 
       <tr> 
        @foreach($keys as $key) 
         <th>{!! $key !!}</th> 
        @endforeach 
       </tr> 
      </thead> 
      <tbody> 
       @foreach($contents as $content) 
        <tr> 
         @foreach($keys as $key) 
          <td>{!! $content[ $key ] !!}</td> 
         @endforeach 
        </tr> 
       @endforeach 
      </tbody> 
     </table> 
    </body> 
</html> 

我的控制器:

Excel::create('locations-' . date('Y-m-d'), function ($export_file) { 
    $export_file->sheet('Locations', function($sheet) { 
     $sheet->loadView('admin.layout.export', Location::getExportData()); 
    }); 
})->download($type); 

地点:: getExportData():

public static function getExportData() { 
    $data[ 'contents' ] = []; 
    $data[ 'keys' ] = [ 
     'control', 
     'name', 
     'type', 
     'address', 
     'longitude', 
     'latitude', 
     'description' 
    ]; 

    $count = Location::count(); 
    if($count > 0) { 
     $off_ex = 1000; 
     for($i = 0; $i < ($count/$off_ex); $i++) { 
      $locations = Location::skip($i * $off_ex)->take($off_ex)->get(); 
      foreach ($locations as $key => $location) { 
       $data[ 'contents' ][] = [ 
        'control' => '', 
        'name' => mb_convert_encoding($location->name, "UTF-8"), 
        'type' => $location->type, 
        'address' => $location->address, 
        'longitude' => $location->longitude, 
        'latitude' => $location->latitude, 
        'description' => $location->description 
       ]; 
      } 
     } 
    } 

    return $data; 
} 

图片:

enter image description here

+0

请张贴您用来生成这种CSV文件,CSV文件本身的代码。 – Jerodev

+0

@Jerodev更新我的问题 –

回答

0

试试包括<meta charset="UTF-8">在视图文件的头部来解决这个问题。

detailed reference

+0

它是一个配置导入?我需要出口 –

+0

@YudiYohanesSeptianGotama对不起!我的错。看到我更新的答案。 –

+0

是的,我包括meta,我用我的代码更新了我的问题 –