By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

problems with streamwriter output to .csv

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Brendan Miller <br************@rogers.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
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.
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Brendan Miller <br************@rogers.com> wrote:
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 Wordpa

d
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 i

t
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.)

Nov 15 '05 #3

P: n/a
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?
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Brendan Miller <br************@rogers.com> wrote:
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 Wordpa

d
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 i

t
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.)

Nov 15 '05 #4

P: n/a
Brendan Miller <ho**********@hotmail.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Brendan Miller <ho**********@hotmail.com> wrote:
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.)


That seemed to work! Thank you very much.

Brendan
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.