I have a StringBuilder and I want to write a text containing accent to an csv file.
Code:
StringBuilder strbr = new StringBuilder();
strbr.AppendLine("ù;é;à");
File.WriteAllText(filePath + ".csv", strbr.ToString());
But when I open my csv file, there is only: é
The file "test.csv' correctly contain ù;é;à, but when I open it with Excel I have:

Maybe I missed a header for Excel?
The file will be saved with UTF-8 encoding, but read with default one, say Win-1251. You can explicitly specify an encoding (UTF8 in this case):
File.WriteAllText(filePath + ".csv", strbr.ToString(), Encoding.UTF8);
The actual problem is the absence of BOM (Bite Order Mark): by default File.WriteAllText writes the text in the UTF8 format without BOM:
https://referencesource.microsoft.com/#mscorlib/system/io/file.cs,8a8ede9e1ec4fece
public static void WriteAllLines(String path, IEnumerable<String> contents)
{
// ...
InternalWriteAllLines(new StreamWriter(path, false, StreamWriter.UTF8NoBOM), contents);
}
then Excel reads the file, doesn't see any BOM and thus tries to read the file using the default encoding.
thanks to Patrick Hofman for clarifying something which was wrong in my initial answer
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