0

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

2
  • Please post the code you used to generate this csv file and the csv file itself. Commented Dec 5, 2016 at 10:10
  • @Jerodev updated my question Commented Dec 5, 2016 at 11:03

1 Answer 1

1

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

In detailed reference

Sign up to request clarification or add additional context in comments.

3 Comments

is it a config to import? i need to export
@YudiYohanesSeptianGotama Sorry! My mistake. See my updated answer.
yes i include meta on it, i updated my question with my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.