I want to write something like
=HYPERLINK("http://example.com"; "Example")
to a comma-separated CSV file, but Excel parses the semicolon and puts "Example") part in another cell. I tried escaping the semicolon with backslash and wrapping everything in double quotes without any luck.
Any help?
Actually, the only value to escape is double quotes symbol. All other cell content gets into it and displayed correctly in Excel. Checked with various versions of Excel and ODBC CSV parsers in Cyrillic locale under Windows.
In North America and some other countries, the default list separator is a comma, so you get CSV comma delimited. In European countries, a comma is reserved for the decimal symbol, and the list separator is generally set to semicolon. That is why the result is CSV semicolon delimited.
By default, the escape character is a " (double quote) for CSV-formatted files. If you want to use a different escape character, use the ESCAPE clause of COPY , CREATE EXTERNAL TABLE or gpload to declare a different escape character.
The wrapping with double quotes was already the correct idea, but you have to make sure you do it correctly. You can put a column within double quotes, then everything inside is considered as a single value. Quotes itself have to be escaped by writing two of them (""
).
See for example this:
Column A;Column B;Column C Column A;"Column B; with semicolon";Column C Column A;"Column B"";"" with semicolon and quotes";Column C Column A;"=HYPERLINK(""http://example.com""; ""Example"")";Column C
I also had a very wild time tring to figure the whole picture out, then is how I've got all my csv ready to open into excel in php ( which includes utf8 encoding as well ) :
$sep='";"';//note the separator is double quoted while($t=mysql_fetch_assoc(mysql_query('select ..')){ #replaces the caracters who are breaking csv display $t=array_map(function($x){return str_replace(array("\n","\r",'"'),array('\\n',"\\r",'""'),$x);},$t); $csv.="\n\"".implode($sep,$t)."\""; } $charset='utf-8'; header('Content-Type: application/csv;charset='.$charset); header('Content-Disposition: attachment; filename="filename.csv"'); $bom=chr(239).chr(187).chr(191);#this tells excel document is utf8 encoded die($bom.$csv);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With