Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When exporting to a CSV with unicode characters, contents can become garbled #3427

Closed
zhanghw89 opened this issue Apr 7, 2020 · 12 comments
Closed
Labels
bug Undesired behaviour confirmed Bug is confirm by dev team resolved A fixed issue
Milestone

Comments

@zhanghw89
Copy link

Describe the bug

when i export a csv with Chinese word ,it has a Chinese garbled. the version is V1.2.10

@zhanghw89 zhanghw89 added bug Undesired behaviour unverified Some days we don't have a clue labels Apr 7, 2020
@netniV
Copy link
Member

netniV commented Apr 7, 2020

Please give more description and steps that you have taken.

@TheWitness
Copy link
Member

Seems we've seen this one before. The CSV output is likely not UTF8.

@TheWitness
Copy link
Member

Here is one article: https://stackoverflow.com/questions/4348802/how-can-i-output-a-utf-8-csv-in-php-that-excel-will-read-properly

Then I found this on secure.php.net

if you want make UTF-8 file for excel, use this:

$fp = fopen($filename, 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

@TheWitness
Copy link
Member

And this one looks much more elegant:

   
    $data = "Some utf-8 characters ąćżźćł" 
     
    header("Content-Type: application/octet-stream"); 
    header("Content-Transfer-Encoding: binary"); 
    header('Expires: '.gmdate('D, d M Y H:i:s').' GMT'); 
    header('Content-Disposition: attachment; filename = "Export '.date("Y-m-d").'.tsv"'); 
    header('Pragma: no-cache'); 
     
    //these characters will make correct encoding to excel 
    echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data); 

@TheWitness TheWitness changed the title when export a csv with Chinese word garbled When export a csv with Chinese word garbled Apr 9, 2020
@TheWitness
Copy link
Member

Please test the latest commit in the 1.2.x branch for graph_xport.php. Excel does not detect it as CSV any longer, at least not automatically, but it still looks right.

@TheWitness TheWitness added resolved A fixed issue confirmed Bug is confirm by dev team and removed unverified Some days we don't have a clue labels Apr 9, 2020
TheWitness added a commit that referenced this issue Apr 9, 2020
When export a csv with Chinese word garbled
@TheWitness
Copy link
Member

Any feedback?

@TheWitness
Copy link
Member

@zhanghw89, does it work for you?

@zhanghw89
Copy link
Author

I finally solved the problem based on this url. https://kindevil.com/archives/552.html

@TheWitness
Copy link
Member

Please upload your modified file.

@netniV
Copy link
Member

netniV commented Apr 26, 2020

@TheWitness, I would agree with the diagnosis and fix so far. If it needs a further review, tag me.

@TheWitness
Copy link
Member

I'm not happy with the fix at the point. It's able to export, but Excel no longer thinks the file is CSV, and you have to use text to columns to have Excel process the file like a normal ASCII CSV file.

TheWitness added a commit that referenced this issue Apr 26, 2020
When export a csv with Chinese word garbled
@TheWitness
Copy link
Member

Just went to the site recommend, made the one small update, and now it's loading as CSV just fine with the correct encoding.

@netniV netniV changed the title When export a csv with Chinese word garbled When exporting to a CSV with unicode characters, contents can become garbled May 3, 2020
@netniV netniV added this to the 1.2.12 milestone May 4, 2020
@netniV netniV closed this as completed May 4, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Undesired behaviour confirmed Bug is confirm by dev team resolved A fixed issue
Projects
None yet
Development

No branches or pull requests

3 participants