£ symbol in csv file for Excel - Encoding weirdness - HELP!
-
Hi all. We have a C# 2.0 prog writing to a csv file for emailing bills to customers. So, we have the contents of the csv file in a string. We use a stream writer to send it to the file. We open the file in Excel (2003) and the £ symbol is now prepended with Ậ Seems the encoding has turned the £ into UTF-8 and stuffed and extra charachter in there, that Excel renders incorrectly. We tried everything, Encoding in various ways, using File.WriteText, converting to bytes using Encoding, nothing worked as expected. In the end we settled on this:
byte[] bArr = new byte[str.Length]; for (int idx = 0; idx < str.Length; idx++) { char c = str[idx]; byte b = (byte)c; bArr[idx] = b; } File.WriteAllBytes(csvFileName, bArr);
Pretty nasty, really. So, question: What is the correct encoding level/object/type to use to do this more elegantly? Thanks in advance. -
Hi all. We have a C# 2.0 prog writing to a csv file for emailing bills to customers. So, we have the contents of the csv file in a string. We use a stream writer to send it to the file. We open the file in Excel (2003) and the £ symbol is now prepended with Ậ Seems the encoding has turned the £ into UTF-8 and stuffed and extra charachter in there, that Excel renders incorrectly. We tried everything, Encoding in various ways, using File.WriteText, converting to bytes using Encoding, nothing worked as expected. In the end we settled on this:
byte[] bArr = new byte[str.Length]; for (int idx = 0; idx < str.Length; idx++) { char c = str[idx]; byte b = (byte)c; bArr[idx] = b; } File.WriteAllBytes(csvFileName, bArr);
Pretty nasty, really. So, question: What is the correct encoding level/object/type to use to do this more elegantly? Thanks in advance.A would guess that Excel is reading the file as an ANSI file, that is/was the standard text format in windows prior to unicode. Try the
Encoding.Default
encoding, that will give you the encoding currently used by the system for ANSI files.--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams