StreamWriter.WriteLine() function is generating a "Â" character
I am writing some strings in an Excel file. Sometimes the call to the
StreamWriter.WriteLine()
function unexpectedly creates a "Â" character.
Any idea why?
Update
the code:
StreamWrit开发者_JAVA百科er writer = new StreamWriter(File.Create(outFile));
string headerline = "";
foreach (DataColumn colum in reportContents.Columns)
{
headerline = headerline + '"' + row[colum].ToString() + '"' + ',';
}
writer.WriteLine(headerline);
the output: Personal Protection |Post-Retirement Savings|Pre-Retirement Pension|Tax & Estate Planning
Expected output: Personal Protection |Post-Retirement Savings|Pre-Retirement Pension|Tax & Estate Planning
I get the solution: just i need to specify the default the encoding in StreamWriter like as follows and it works.
StreamWriter writer = new StreamWriter(File.Create(outFile), Encoding.Default);
shuvra
It isn't actually creating an Â
character - it's just writing data in a different encoding. If you look at the StreamWriter
constructor overloads (http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx) you can indicate which encoding you want the StreamWriter
to write it's data in.
In case you haven't dealt with encoding before: Joel wrote a good article about it at http://www.joelonsoftware.com/articles/Unicode.html
The character set of your code should be "utf-8".
精彩评论