How to write a value which contain comma to a CSV file in c#?

前端 未结 6 1478
时光取名叫无心
时光取名叫无心 2021-01-01 14:58

I am using a data table for storing data.

I am exporting the data from data table to CSV file.

Sometimes there may be values containing comma(,)

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 15:31

    StringBuilder sb = new StringBuilder();          
            foreach (DataColumn col in dt.Columns)
            {
                if (col.ColumnName.Contains(","))
                {
                    sb.Append(String.Format("\"{0}\",", col.ColumnName));
                }
                else
                {
                    sb.Append(String.Format("{0},", col.ColumnName));
                }
            }
    

提交回复
热议问题