Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a CSV file for Mac users with PHP
    primarykey
    data
    text
    <p>I use a generic algorithm to write CSV files that in theory works for the major OSes. However, the client started to use Mac a few weeks ago, and they keep telling me the CSV file cannot be read in Microsoft Excel 2008 for Mac 12.2.1.</p> <p>They have their OS configured to use "semicolon ;" as list separator, which is exactly what I am writing in the CSV. They also say, that when they open the file in notepad, they have noticed there are no linebreaks, everything is displayed in a single line; which is why Excel cannot read the file properly; but in my code, I am using the cross-browser line break <code>\r\n</code> </p> <p>This is the full code I use:</p> <pre><code>header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Output to browser with appropriate mime type, you choose ;) header("Content-type: text/x-csv"); //header("Content-type: text/csv"); //header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=participantes.csv"); $separator = ";"; $rs = $sql-&gt;doquery("SELECT A QUERY TO RETRIEVE DATA FROM THE DB"); $header = ""; $num_fields = mysql_num_fields($rs); for($i=0; $i&lt;$num_fields; $i++){ $field = mysql_field_name($rs, $i); $header .= $field.$separator; } echo $header."\r\n"; while($row = $sql-&gt;fetch($rs)){ $str = ""; for($i=0; $i&lt;$num_fields; $i++){ $field = mysql_field_name($rs, $i); $value = str_replace(";", ",", $row-&gt;{$field}); $value = str_replace("\n", ",", $value); $value = str_replace("\d", ",", $value); $value = str_replace(chr(13), ",", $value); $str .= $value.$separator; } echo $str."\r\n"; } </code></pre> <p>Is there anything I can do so Mac users can read the file properly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload