-1

So I'm trying to export a csv using PHP in which the contents contains UTF-8 character and I want the resultant csv to open in Excel smoothly (including Mac excel)

So there is an answer here: How can I output a UTF-8 CSV in PHP that Excel will read properly?

Checkout the top answer.

But then in order to implement that you need to use tabs to separate the fields instead of commas...Is there a way to achieve this while still using commas and not tabs and still have it work in OS X

EDIT

Mostly to Mark Baker but everyone feel free to comment

Another code update

while(@ob_end_clean());
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=fileexport.csv");
echo "\xEF\xBB\xBF";
print "sep=,\n";
print $output;
exit;
Community
  • 1
  • 1
pillarOfLight
  • 8,592
  • 15
  • 60
  • 90
  • With a BOM headingto the file, and by using a `sep=,` as the first line of the file? – Mark Baker Mar 05 '14 at 16:37
  • But simply setting http response headers to say it is UTF-8 will have absolutely no effect whatsoever – Mark Baker Mar 05 '14 at 16:38
  • will that work with Mac OS X excel – pillarOfLight Mar 05 '14 at 17:09
  • Why don't you try it and see? – Mark Baker Mar 05 '14 at 17:12
  • will it theoretically work? i'm not gonna bother with something that's not even supposed to work in theory....also check this out: http://stackoverflow.com/questions/20395699/sep-statement-breaks-utf8-bom-in-csv-file-which-is-generated-by-xsl ... do you print out sep=, first or the BOM heading first...also isn't sep=, outside the csv standard? – pillarOfLight Mar 05 '14 at 17:14
  • Theoretically it will work, otherwise I wouldn't have suggested it; but I don't have a Mac that I can actually test it on – Mark Baker Mar 05 '14 at 17:37
  • BOM should __always__ be the first bytes in the file; where sep= applies, it should be the first line of the file.... and yes, it's outside the original CSV standard... the standard has evolved.... and it's not likely to work with older versions of Excel, which is why I prefer building BIFF or OfficeOpenXML files, because they never have this problem – Mark Baker Mar 05 '14 at 17:38
  • @MarkBaker It didn't work checkout my code in the question – pillarOfLight Mar 06 '14 at 00:12
  • Why are you converting from UTF-8 to UTF-16LE? Why not simply write UTF-8 with a UTF-8 BOM? – Mark Baker Mar 06 '14 at 00:17
  • @MarkBaker checkout the update once again....this time the outputted utf8 characters dont get converted...note that if I get rid of the print "sep=,\n" line it works...but mac isn't going to accept this – pillarOfLight Mar 06 '14 at 00:27
  • OK, you've convinced me that it doesn't work, and that I'm right to use native Excel formats rather than CSV – Mark Baker Mar 06 '14 at 00:28

1 Answers1

0

fputcsv should work fine in this instance. Take the following example, where as the third parameter of fputcsv is the delimiter. By default it is , (comma), but you could also use "\t" for tab files. CSV files should be interpreted the same on either OS

if( $fh = fopen("output_file.csv","w") ){

    $put = array("column1, with comma","column2, with comma","column3" /*,"columnN"*/);

    fputcsv($fh,$put,",");

    fclose($fh);
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62