Writing csv file to asp.net

I am trying to export data to a csv file, since there are Chinese characters in the data that I need to use unicode .. but after adding the preamble for unicode, the commas are not recognized as delimiters, and all the data is now written in the first column. I'm not sure what happened. Below is my code that I wrote in the .ashx file.

DataView priceQuery = (DataView)context.Session["priceQuery"]; String fundName = priceQuery.Table.Rows[0][0].ToString().Trim().Replace(' ', '_'); context.Response.Clear(); context.Response.ClearContent(); context.Response.ClearHeaders(); context.Response.ContentType = "text/csv"; context.Response.ContentEncoding = System.Text.Encoding.Unicode; context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fundName + ".csv"); context.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); String output = fundName + "\n"; output += "Price, Date" + "\n"; foreach (DataRow row in priceQuery.Table.Rows) { string price = row[2].ToString(); string date = ((DateTime)row[1]).ToString("dd-MMM-yy"); output += price + "," + date + "\n"; } context.Response.Write(output); 
+6
c # csv export-to-csv
source share
1 answer

It seems that MS Office does not support Unicode csv files.

But some users have reported that the tab delimiter is working.

I would try UTF-8 rather than Unicode, this may cause the delimiters to be treated as ASCII characters.

+4
source share

All Articles