ASP.NET C# export data into CSV format, double quotes not preserved

VuDo0

New Member
I'm trying to export some data to CSV with ASP.NET C#.
I use this code: \[code\]private void writeCSVfile(DataTable theData, string fileName){ StringBuilder dataString = new StringBuilder(); string currentId = Guid.Empty.ToString(); HttpContext context = HttpContext.Current; context.Response.Clear(); foreach (DataColumn column in GTDSSearchData.Columns) { if (column.ColumnName.ToString().ToLower() != "the_id") { dataString.Append(column.ColumnName + ","); } } dataString.Remove(dataString.Length - 1, 1); dataString.Append(Environment.NewLine); foreach (DataRow row in GTDSSearchData.Rows) { currentId = row[1].ToString(); for (int i = 1; i < GTDSSearchData.Columns.Count; i++) { dataString.Append(row.ToString().Replace(",", string.Empty) + ","); } dataString.Remove(dataString.Length - 1, 1); dataString.Append(Environment.NewLine); } context.Response.Write(dataString.ToString()); context.Response.ContentType = "text/csv"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); context.Response.End();}\[/code\]My dataString is \[code\]test1, 1 "hello world", 10 test2, 2\[/code\]The result CSV file has these content\[code\]test1, 1 hello world, 10 test2, 2\[/code\]Do you know why my double quotes go away and how to preserve them when exporting to CSV file.Thanks a lot.
 
Back
Top