Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
There is some problem while I want to export data to csv file:
C#
public void ExportLoalDataToCSV(DataTable dt, string fileName)
{
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
    IEnumerable<string> query = (from row in dt.AsEnumerable()
                                 select row.Field<string>("ID") + "," + row.Field<string>("Text")).Distinct();
    foreach (string row in query)
    {
        string[] temp = row.Split(',');
        foreach (string item in temp)
        {
            sw.Write(item + ",");
        }
    }
    sw.Close();
}

while if row.Field<string>("Text") contains a ',', there will be another extra column which is not my expected. :(
(From the code, I just want to get 2 columns, ID and Text)
Is there any way to reslove this problem?

Thanks a lot.
Jessie
Posted
Updated 12-Jan-18 8:06am
v2

Either come up with an escape sequence for comma e.g. &comma& then replace this with , after the split.

or put each field in '' quotes and parse the string to ignore commas in between quotes

or use a more unlikely character to separate your fields e.g. ^ instead of comma
 
Share this answer
 
v2
The solution I have found to similar problems was to insert a BOM character to the .CSV file to indicate its encoding.
CSV and BOM character - CSV[^]
If you even inserted a UNICODE character to a Notepad file, and tried to save it, you would have seen a warning that the contents of the file will be lost, it is the same with .CSV file which are plain text files in general. The default encoding is ASCII and you need to change it to UTF-8.
 
Share this answer
 
Comments
Richard Deeming 12-Jan-18 14:17pm    
Adding a BOM to the file won't change the fact that a comma in an unquoted string will still be seen as a delimiter, splitting the field into two.

And what filter are you using on the question list if a question from 2010 appeared at the top?!
Michael Haephrati 12-Jan-18 14:30pm    
Richard, I reported that as a bug https://www.codeproject.com/suggestions.aspx?msg=5475847#xx5475847xx.
This questions appeared on the top of my list!
Please see my screenshot 2018-01-12__7_.png (549.9 KB)
As you can see, this question appears on top of my list and the only filters I can see are: "Active" (so this question is still marked as Active), and "UNICODE"
Richard Deeming 12-Jan-18 14:34pm    
Well, it's appearing at the top of your list now because it was updated by your answer. :)

It doesn't look like the question has been edited, and there don't seem to have been any solutions removed, which are the main reasons why it would have popped up in the "recent" list.

If it was showing within the first few pages of the "active" or "unanswered" lists prior to your answer, then I suspect there might be a bug lurking.
Michael Haephrati 12-Jan-18 14:37pm    
... and yet I got it in the first place. I definitely wasn't looking for old questions
Richard Deeming 12-Jan-18 14:37pm    
Definitely sounds like a bug.
You could try wrapping your strings in quotations:

IEnumerable<string> query = 
(
     from row in dt.AsEnumerable()
     select row.Field<string>("ID") 
          + ", \""
          + row.Field<string>("Text") 
          + "\""
).Distinct();


Also, since you are including the comma as a part of your selection query, I don't think you will need to split the string before writing it out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900