-1

I have export to csv function which is in export.php. The function is working fine. It is just the display of Japanese character is showing like this: 予約済み Both when i echo the variable in the page and in csv file.

charset is already in UTF-8. It is displaying fine in other pages. I don't know what to do next.

Here's my code:

public function excel()
{
    $student_id = $_GET['student_id'];
    $lesson = &Chico::getInstance('Controller_Frontend_Lesson');
    $defaults = &Chico::getInstance('Model_Defaults');
    $get_lesson_history = $lesson->lesson_history(get_user_meta($student_id, 'gge_access_token', true));
    $convert_lesson_time = $lesson->convert_lesson_time($get_lesson_history);
    $lesson_history = $lesson->sort_history($convert_lesson_time);
    $count = 1; $lesson_count = $this->get_lesson_count($lesson_history);

    header('Content-Type: text/csv;charset=utf-8'); 
    header(sprintf('Content-Disposition: attachment;Filename=%s.csv', get_user_meta($student_id, 'gge_name', true)));

    $outstr  = sprintf('PERIOD, %s ~ %s %s', !empty($_GET['begin']) ? $_GET['begin'] : 'Beginning', !empty($_GET['end']) ? $_GET['end'] : 'Present', "\n");
    $outstr .= sprintf('STUDENT NAME, %s %s', get_user_meta($student_id, 'gge_name', true), "\n");
    $outstr .= sprintf('SKYPE ID, %s %s', get_user_meta($student_id, 'gge_skype', true), "\n");
    $outstr .= sprintf('NUMBER OF BOOKINGS, %s %s', $lesson_count, "\n");
    $outstr .= sprintf('NUMBER OF LESSONS, %s %s', $lesson_count, "\n\n");

    $outstr .= '#, DATE/TIME, TEACHER, STATUS, LESSON, REQUEST, COMMENT';
    $outstr .= sprintf('%s', "\n");

    if(!empty($lesson_history) && is_array($lesson_history)):
        foreach ($lesson_history as $key => $value) { 
            $lastkey  = count($value)-1;
            $launched = strtotime($value[$lastkey]['launched']);
            $datetime = sprintf('%s %2$s:00 ~ %2$s:50', date(__(get_option( 'date_format' )), $launched), date(__('H'), $launched));
            if(strtotime($_GET['begin']) <= $launched && strtotime(sprintf('%s+1 days', $_GET['end'])) >= $launched):
                $outstr.= join(',', array($count, $datetime, 
                                            $value[$lastkey]['teacher_name'],
                                            $defaults::$status_ja[$value[$lastkey]['status']], 
                                            $value[$lastkey]['request_title'],
                                            $value[$lastkey]['request_body'],
                                            $value[$lastkey]['scores']['message']));
                $outstr .= sprintf('%s', "\n");
                $count++;
            endif;
        } 
    else: 
            $outstr.= join(',', 'No lesson history');
    endif;
    echo $outstr;
}

Here's the data in csv: STATUS LESSON SAMMY 予約済み KREAN 予約済み

Code Diary
  • 417
  • 2
  • 7
  • 17
  • Please provide more information about what appears how where. Are you possibly opening the file in Excel? Have you set headers? – deceze Apr 01 '14 at 10:18
  • try outputting the header `header('Content-Type: text/csv;charset=utf-8'); ` – iamsleepy Apr 01 '14 at 10:22
  • Hi @iamsleepy, I tried, still the same. – Code Diary Apr 01 '14 at 10:25
  • possible duplicate of [How can I output a UTF-8 CSV in PHP that Excel will read properly?](http://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly) – deceze Apr 01 '14 at 10:28

1 Answers1

2

Excel opens CSV files by default using windows-1252 encoding. Try to open your CSV file with a text editor or with Open Office (in which you can choose the character set).

ôkio
  • 1,772
  • 1
  • 15
  • 16
  • 1
    I am using ubuntu and opening the csv file in libre office. I didn't notice the option for Character Set. It was set to Western. T___T I changed and now it's working. Arigatou gozaimasu! – Code Diary Apr 01 '14 at 10:30