I need to write the content of a DataTable
to a csv. There are some Chinese strings in it as well.
I used to use the below script:
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(dlg.FileName);
string strHeader = "";
for (int s = 0; s < table.Columns.Count; s++)
{
strHeader += table.Columns[s].ColumnName + ",";
}
streamWriter.WriteLine(strHeader);
for (int m = 0; m < table.Rows.Count; m++)
{
string strRowValue = "";
for (int n = 0; n < table.Columns.Count; n++)
{
strRowValue += table.Rows[m][n] + ",";
}
streamWriter.WriteLine(strRowValue);
}
streamWriter.Close();
But that obviously doesn't handle Unicode characters. It writes instead something like æœç‹—
.
So I tried to set the encoding like this:
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(dlg.FileName, Encoding.UTF8);
But then I get Argument 1: cannot convert from 'string' to 'System.IO.Stream
. So how could I set the encoding in this very example?