Gia hạn câu trả lời @Vinicious.
Tôi có dữ liệu có thể chứa dấu phẩy. Giải pháp phổ biến là để thoát khỏi đoạn dữ liệu đó bằng cách kèm theo nó trong dấu ngoặc kép, đồng thời đảm bảo thoát khỏi các dấu ngoặc kép cũng có thể là một phần của dữ liệu.
Một chà tôi đã phản đối và cảnh báo khi viết CSV, excel sẽ không thích bạn nếu bạn đặt dấu cách theo dấu phẩy của bạn.discovered solution to my problem from superuser answer
protected void btnDownload_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms, System.Text.Encoding.UTF8);
var structures = KAWSLib.BusinessLayer.Structure.GetStructuresInService();
// *** comma delimited
tw.Write("Latitude, Longitude, CountySerial, StructureType, Orientation, District, RoutePre, RouteNo, LocationDesc");
foreach (var s in structures)
{
tw.Write(Environment.NewLine + string.Format("{0:#.000000},{1:#.000000},{2},{3},{4},{5},{6},{7},{8}", s.LATITUDE, s.LONGITUDE, s.CO_SER, EscapeIfNeeded(s.SuperTypeLookup.SHORTDESC), EscapeIfNeeded(s.OrientationLookup.SHORTDESC), s.DISTRICT, s.ROUTE_PREFIX, s.RouteValue, EscapeIfNeeded(s.LOC_DESC)));
}
tw.Flush();
byte[] bytes = ms.ToArray();
ms.Close();
Response.Clear();
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=" + string.Format("kaws-structures-{0:yyyy.MM.dd}.csv", DateTime.Today));
Response.BinaryWrite(bytes);
Response.End();
}
string EscapeIfNeeded(string s)
{
if (s.Contains(","))
{
return "\"" + s.Replace("\"", "\"\"") + "\"";
}
else
{
return s;
}
}
Dưới đây sẽ gây ra sự cố cho excel. Trong excel, báo giá đầu tiên sẽ trở thành một phần của dữ liệu và do đó tách biệt với dấu phẩy được nhúng. Không gian xấu.
tw.Write(Environment.NewLine + string.Format("{0:#.000000}, {1:#.000000}, {2}, {3}, {4}, {5}, {6}, {7}, {8}", s.LATITUDE, s.LONGITUDE, s.CO_SER, EscapeIfNeeded(s.SuperTypeLookup.SHORTDESC), EscapeIfNeeded(s.OrientationLookup.SHORTDESC), s.DISTRICT, s.ROUTE_PREFIX, s.RouteValue, EscapeIfNeeded(s.LOC_DESC)));
http://www.xefteri.com/articles/show.cfm?id=8 – fejesjoco