0

There might be similar question, but I am sorry to say that I couldn't find that. So let me ask about my problem.

I have a PHP function to download CSV file from browser as below:

function outputCSV($data, $file_name = 'file.csv') {
    header('Content-Encoding: UTF-8');
    # output headers so that the file is downloaded rather than displayed
    header('Content-type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename=$file_name");
    header('Content-Transfer-Encoding: binary');
    # Disable caching - HTTP 1.1
    header("Cache-Control: no-cache, no-store, must-revalidate");
    # Disable caching - HTTP 1.0
    header("Pragma: no-cache");
    # Disable caching - Proxies
    header("Expires: 0");

    # Start the ouput
    $output = fopen("php://output", "w");
    fputs($output, "\xEF\xBB\xBF"); // UTF-8 BOM !!!!!
    # Then loop through the rows
    foreach ($data as $row) {
        # Add the rows to the body
        fputcsv($output, $row); // here you can change delimiter/enclosure
    }
    # Close the stream off
    fclose($output);
}

After downloading the file, while open using Windows 10 Excel app the Japanese characters are wired. But can be viewed properly using Text Editor like NotePad++.

Is there any help on this?

Thanks.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
gsmaker
  • 533
  • 1
  • 4
  • 21
  • I think there's an option in Excel that lets you decide which encoding to use while opening the file, last I remember. I might also be the way it's saved. I think this may be a localized issue. – Funk Forty Niner Jan 04 '19 at 04:32
  • May be can try `header('Content-Type: application/octet-stream');` and ignore `header('Content-Transfer-Encoding: binary');`, `fputs($output, "\xEF\xBB\xBF"); // UTF-8 BOM !!!!!` – Nick Wang Jan 04 '19 at 04:44
  • @incNick : Have tried according to your suggestion, but no luck – gsmaker Jan 04 '19 at 05:23
  • @gsmaker oh :( so, try other way, use iconv (like `iconv("UTF-8", "Windows-1252", $csv);` translate it to ANSI format. The UTF-8 bom is invalid for Excel and Excel always open csv file by ANSI code. – Nick Wang Jan 04 '19 at 06:19
  • if not, you must use import data method import data from the downloaded csv file for Excel. – Nick Wang Jan 04 '19 at 06:21
  • @incNick: Data import into Excel goes fine. But your iconv conversion not worked. – gsmaker Jan 04 '19 at 08:03

0 Answers0