Export de fichier CSV pour Excel avec support de caractères spéciaux (Chinois, ...~)
Par Ivan solart, vendredi 22 octobre 2010 à 10:29 :: Développement PHP :: #47 :: rss
Exporter sa liste de contact stockées en base de données en un fichier CSV afin de la traiter sour Excel ?
Ci-dessous voici une suggestion de code Note: veuillez porter attention à la ligne de code
Plus d'information sur les differents BOM
What is a BOM?
A: A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plaintext files. Under some higher level protocols, use of a BOM may be mandatory (or prohibited) in the Unicode data stream defined in that protocol
Ci-dessous voici une suggestion de code Note: veuillez porter attention à la ligne de code
fwrite( $fp, "\xEF\xBB\xBF", 3 ); // insertion du BOM Excelc'est avec cela que les langues exotiques (中文,...) encodées en UTF-8 s'afficheront correctement.
Plus d'information sur les differents BOM
$name_file = ROOT.'csv/output.csv'; // Chemin absolu du fichier output.csv (fichier vide avec pour extension .csv)
if( $fp = fopen($name_file,'w') ){ // overture du fichier en écriture
fwrite( $fp, "\xEF\xBB\xBF", 3 ); // insertion du BOM Excel
fwrite( $fp, $csv_output, strlen($csv_output) ); // ecriture du fichier (csv_output: données a inserer dans le fichier
// separées par des virgules ',')
fclose($fp); // fermeture du fichier
// headers, fichier de type csv en telechargement
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=".'nomDuFichier_'.date('Ymd').".csv");
$fh = fopen($name_file,'r'); // Ouverture et lecture du fichier precedement cree
fpassthru($fh);
fclose($fh);
}else{
echo 'error while opening file';
die();
}
What is a BOM?
A: A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plaintext files. Under some higher level protocols, use of a BOM may be mandatory (or prohibited) in the Unicode data stream defined in that protocol
Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.