Re: export to csv

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Jim Lucas <lists@xxxxxxxxx> wrote: 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
> 
>  
> 
> > 
>  
> 
> $csv_output = 'column1';
> 
> $csv_output .= "\015\012";
> 
> $result = mysql_query("select * from table"); 
> 
> while($row = mysql_fetch_array($result)) 
> 
>        {
> 
>               $csv_output .= '"'.$row[column1].'"';


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 .= "\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();
> 
>        }
> 
>  
> 
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Thanks Jim. That did the trick.

Haig

       
---------------------------------
Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail  

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux