problems with streamwriter output to .csv

  • Thread starter Thread starter Brendan Miller
  • Start date Start date
B

Brendan Miller

Hi,

I have been having a problem writing the contents of a dataset to a .csv
file. When I write to a .csv file and open it with either Excel or Wordpad
the "french" characters are wrong. When I open the file with notepad the
characters are displayed correctly. e.g. in notepad: École, in
Wordpad/Excel: Ã?cole. Having the user import a text file and format it
themselves is not an option. The output must be .csv.

The function I was using is as follows:

private void CreateFile(string path, DataSet ds, string strXYZ, int
language)
{
string strfname = \\2004 + strXYZ + "_" + language;
string strext = ".txt";
StreamWriter file = new System.IO.StreamWriter(path + strfname +
strext);

try
{
foreach (DataTable dt in ds.Tables)
{

//write column headers
foreach (DataColumn dc in dt.Columns)
{
file.Write(dc.Caption.ToString() + ",");
}

//write each row
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.Caption == "SomeColumnName")
{
string strBarcode;
strBarcode = dr[dc].ToString();
file.Write(strBarcode);
}
else
{
file.Write(dr[dc] + ",");
}
}
file.WriteLine();
Application.DoEvents();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
file.Flush();
file.Close();
}

Thanks in advance.
Brendan
 
Brendan Miller said:
I have been having a problem writing the contents of a dataset to a .csv
file. When I write to a .csv file and open it with either Excel or Wordpad
the "french" characters are wrong. When I open the file with notepad the
characters are displayed correctly. e.g. in notepad: École, in
Wordpad/Excel: Ã?cole. Having the user import a text file and format it
themselves is not an option. The output must be .csv.

But what encoding do you want it to be? Currently you're writing it out
in UTF-8. If you want to change the encoding, you need to specify that
in your call to the StreamWriter constructor.

(It would also be a good idea to put the whole StreamWriter section in
a using statement, so that the file gets closed immediately even if an
exception is thrown.)
 
I figured out I needed to use Encoding.unicode when creating the
streamwriter.

StreamWriter file = new System.IO.StreamWriter(path + strfname +
strext, false, Encoding.Unicode); (for anyone who has a similar
problem)

I'll try using a using statement.

Thanks for the help Jon.
 
The problem i'm having now is that when I use Encoding.Unicode excel
does not see the commas in the file and puts all data for that row in
the first column.
Any Ideas?
 
Brendan Miller said:
The problem i'm having now is that when I use Encoding.Unicode excel
does not see the commas in the file and puts all data for that row in
the first column.
Any Ideas?

I really don't know what Excel is expecting in terms of encoding, I'm
afraid. You could try Encoding.Default - that might work. (It's
different from the default encoding you get by not specifying anything
- slightly confused naming.)
 
Jon Skeet said:
I really don't know what Excel is expecting in terms of encoding, I'm
afraid. You could try Encoding.Default - that might work. (It's
different from the default encoding you get by not specifying anything
- slightly confused naming.)

That seemed to work! Thank you very much.

Brendan
 
Back
Top