Laravel Create CSV that Support Microsoft Office Excel

How to generate CSV files that supported by Microsoft Office Excel?

I created CSV using Laravel/Excel (http://www.maatwebsite.nl/laravel-excel/), the content is japanese character.

Everything is perfect until i open the CSV using Microsoft Office Excel, the japanese charecter not readable by Microsoft Office Excel, i already encode the text to UTF-8 on my view (because i created CSV using loadView() function)

I try to open CSV via notepad and saved it without any changes, and open it again using Microsoft Office Excel, and the japanese character shown.

What happened with it? i opened both file (the default one and the saved csv from notepad) with notepad and check the different, there is no different on the content file.

My View :

<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>

My Controller :

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 );

Location::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;
}

Picture :

enter image description here

Upvotes: 0

Views: 747

Answers (1)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

Try including <meta charset="UTF-8"> in the head of the view file to solve this problem.

In detailed reference

Upvotes: 1

Related Questions