saving data in csv file

2019-06-14 14:30发布

问题:

I have a problem to save data in .csv file.

     void WriteLog(DataRow rzad)
    {
            StreamWriter sw = new StreamWriter("log.csv", true);
            int iColCount = 8;

            for (int i = 0; i < iColCount; i++)
            {
                if (!Convert.IsDBNull(rzad[i]))
                {
                    sw.Write(rzad[i].ToString());
                    sw.Write("\t");
                }
            }
            sw.Write("\n");
            sw.Flush();
            sw.Close();
    }

The problem is tak in file I have data in A column. I want to smash one row in DataRow format to 8 parts, which are put in 8 different columns. My function working as it doesn't see the tab ("\t").

I cant post images so I try to describe results in csv file:

2011-03-17 14:34:11asdPrzekroczono krytyczną minimalną wymaganą wartość parametru5010050080550

This is my example row and I want to smash it to 8 columns:

2011-03-17 14:34:11     asd     Przekroczono krytyczną minimalną wymaganą wartość parametru   50     100    500     80      550     

"#\t#" doesn't help. The results is:

"2011-03-17 18:29:17#   #asd#   #Przekroczono krytyczną, maksymalną, wymaganą wartość parametru#    #560#   #100#   #500#   #80#    #550#   #"

There is some tabulation but my point is that was made no space but a transition to the next cell :(

"\u0008" also doesn't help.

回答1:

First, you say you are writing to a CSV (comma seperated values) file. But, you are really writing to a tab-delimited file. And, you need to write /r/n between lines:

This works:

    StreamWriter sw = new StreamWriter(@"c:\log.csv", true); 
    int iColCount = 8; 
    for (int i = 0; i < iColCount; i++)
    {           
        {
            sw.Write(i.ToString()); 
            sw.Write("\t"); 
        } 
    } 
    sw.Write("\r\n"); 
    sw.Flush(); 
    sw.Close();


回答2:

I'm inclined to agree @Hossein. I think it's an encoding issue. I ran your code exactly as it is on my machine, and it worked perfectly.



回答3:

Just in case you still haven't found a solution since you wrote this question a year ago:

You can use the KBCSV library which is very popular and handles pretty much everything you require. It uses csv by default but can be easily modified to handle tsv.

For a very simple and easily breakable solution you can try: string.Join("\t", rzad) + "\r\n"

This wouldn't take into account fields that contain tabs. If a field contains a tab, it will make this method useless. Typically fields that would contain the delimiter are double quoted and fields that contain double quotes would be double double quoted.

It's not too difficult to implement this, but it would be reinventing the wheel as it has Kent Boogart has already spent many hours taking into consideration a few of the edge cases that I would not be aware of.



回答4:

The following function is used to write the csv file.

public static void WriteCSV(string file, string content)
{
   using (StreamWriter sw = new StreamWriter(file))
   {
      sw.Write(content);
   }
}

Then call this function by

string appendText = "";
for (int i = 0; i < iColCount; i++)
{
     if (!Convert.IsDBNull(rzad[i]))
     {
         appendText += appendText == "" ? rzad[i].toString() : "," + rzad[i].toString();
     }
}
WriteCSV("C:\\out\\out.csv",appendText);

Another simple example for multiple rows, each row is separated by a new line character '\n'.

WriteCSV("C:\\out\\out.csv","a,b,c,d,e,f,g\nh,i,j,k,l,m,n\n");