2016-12-05 77 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,我用我的代碼更新了我的問題 –