473,756 Members | 3,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with streamwriter output to .csv

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(stri ng path, DataSet ds, string strXYZ, int
language)
{
string strfname = \\2004 + strXYZ + "_" + language;
string strext = ".txt";
StreamWriter file = new System.IO.Strea mWriter(path + strfname +
strext);

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

//write column headers
foreach (DataColumn dc in dt.Columns)
{
file.Write(dc.C aption.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(strB arcode);
}
else
{
file.Write(dr[dc] + ",");
}
}
file.WriteLine( );
Application.DoE vents();
}
}
}
catch(Exception ex)
{
MessageBox.Show (ex.Message);
}
file.Flush();
file.Close();
}

Thanks in advance.
Brendan
Nov 15 '05 #1
5 18899
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
I figured out I needed to use Encoding.unicod e when creating the
streamwriter.

StreamWriter file = new System.IO.Strea mWriter(path + strfname +
strext, false, Encoding.Unicod e); (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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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
The problem i'm having now is that when I use Encoding.Unicod e 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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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
Brendan Miller <ho**********@h otmail.com> wrote:
The problem i'm having now is that when I use Encoding.Unicod e 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.Defaul t - that might work. (It's
different from the default encoding you get by not specifying anything
- slightly confused naming.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
Brendan Miller <ho**********@h otmail.com> wrote:
The problem i'm having now is that when I use Encoding.Unicod e 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.Defaul t - 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1199
by: CMG | last post by:
I am trying to run gocr from my program, and use the output. I have done this with success befor, with a workaround command line tool.: Private irfanview As String = "C:\program files\IrfanView\i_view32.exe" Private startuppath As String = Environment.CurrentDirectory & "\" Private pngimage As String = startuppath & "verify.png" Private png2pnm As String = startuppath & "png2pnm.exe" Private gocr As String = startuppath & "gocr.exe"...
2
2281
by: Bonnett | last post by:
I have made a simple console application that creates a webpage from my playlist, then uploads it to my webhost. I decided to convert it to a windows service and use the FileSystemWatcher to monitor the playlist file, this is where i became stuck. I know the service detects changes to the playlist as when running the Change event of FileSystemWatcher, I add an item to the event log. I've tried making the service without including the...
0
1296
by: CMG | last post by:
I am trying to run gocr from my program, and use the output. I have done this with success befor, with a workaround command line tool.: Private irfanview As String = "C:\program files\IrfanView\i_view32.exe" Private startuppath As String = Environment.CurrentDirectory & "\" Private pngimage As String = startuppath & "verify.png" Private png2pnm As String = startuppath & "png2pnm.exe" Private gocr As String = startuppath & "gocr.exe"...
6
10217
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error message: "The process cannot access the file "whatever" because it is being used by another process." I've even tried opening another file using the same streamwriter object before deleting the original file, but it's no use. Something keeps...
4
8427
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
2
3110
by: Thelonious Monk | last post by:
I have a problem where some data is being eliminated. The problem is that the data contains signed numeric fields (the low-order byte of a negative number uses the first 4 bits as a sign and the last 4 bits as the low-order digit. This produces byte values higher than X'7F'. To be more specific these values are hexadecimal X'B0' through X'B9'. It appears the program is eliminating any byte values greater than X'7F', thus shortening...
7
2958
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I'm building an ASP.NET site which is hosted on a remote server. When I try to create a new file using StreamWriter I must specify the drive location where the file will be saved. I don't have file access anywhere on the remote server, and therefore I can't figure out how I can create a file. Is it possible to utilize StreamWriter to create a new file and somehow display it for the user to save, without actually saving the file...
1
5834
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up. The variables are declared as follows:...
1
4839
by: Claire | last post by:
Hi, I'm trying to run mysqldump.exe from my c# application, running on windows vista with visual studio 2005. The following code does not produce the output file. The same line used in a command prompt works fine. While debugging, I get an exception "InvalidOperationException, StandardOut has not been redirected or the process hasn't started yet." when attempting the System.Console.Write(p.StandardOutput.ReadToEnd() line. I get no...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.