Tuesday, November 1, 2016

האפשרות ש - Microsoft Excel יוכל לקרוא קובץ CSV ב - UTF-8

עבודה עם אקסל יכולה להיות מאוד מרגיזה לפעמים.
לצערנו Excel עדיין עובד בברירת המחדל ב - Windows-1255 Encoding.
אבל מה אם אנחנו רוצים ליצור קובץ CSV מתקדם יותר ... נניח UTF8.
הפתרון נמצא ב - BOM:

 header("Content-Type: text/csv; charset=UTF-8");
 header("Content-Disposition: attachment; filename=$filename"); // Disable caching
 header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
 header("Pragma: no-cache"); // HTTP 1.0
 header("Expires: 0"); // Proxies

 $out = fopen('php://output', 'w');
 $BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
 fwrite($out, $BOM);
 $headlines = array("a", "b", "c");
 fputcsv($out, $headlines);

 // loop data here

 fclose($out);

No comments:

Post a Comment