Re: export to csv
| From: | Jim Lucas | Date: | Wed, 06 Jun 2007 05:57:13 +0000 |
| Subject: | Re: export to csv | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-256155@lists.php.net to get a copy of this message | ||
Haig (Home) wrote:
Hi everyone, I have a small problem when exporting mysql into csv format. The export works fine. The problem is, if the mysql table has a carriage return, opening the csv file in excel will display a square box where the carriage return is. Displaying the table on a web page is fine. Thanks for any help. Haig <?php $csv_output = 'column1'; $csv_output .= "\015\012"; $result = mysql_query("select * from table"); while($row = mysql_fetch_array($result))In a more recent post, the op asked a question that is related to this, in a way. Here is what I think your solution would look like $data = preg_replace("!\r\n|\n|\r!", "\r\n", $row['column1']); $csv_output .= '"' . $data . '"'; Reason being is that Windows uses "\r\n" for line endings. More than likely you are outputting "\n" This would cause windows to display one of those funky boxes that you are talking about. PS. be sure and use quotes around your array key names, otherwise you might cause a PHP Notice on different systems.{$csv_output .= '"'.$row[column1].'"';
$csv_output .= "\015\012";}header("Content-type: application/vnd.ms-excel"); header("Content-disposition: attachment; filename=" . date("Y-m-d")."_my_report".".csv"); print $csv_output; exit; mysql_close();}