用换行符将数据导出为CSV

我试图在C#中将数据导出为CSV文件,但是当我试图在Excel中导入csv文件时,问题就开始了。

在Excel中,我使用“从文本导入”function,然后我将分隔符设置为分号

我的问题是,有些列有换行符,然后在Excel中导入是错误的。

我已经尝试过与单和双打引号没有运气。

我已经find了一个解决scheme,但还没有find。

任何人都知道如果lumenworks有导出function,因为我用这个导入function

问题是导出function,换行符是必需的。

if (list.Any()) { result = list.Select(i => new { i.Product.ProductIdentifier, i.Product.Header, body = string.Format("\"" + "{0}" + "\"", i.Product.Body), Active = i.Product.Active ? 1 : 0, Approved = i.Product.Approved ? 1 : 0, i.Product.Sort, i.Product.MetaDescription, i.Product.MetaKeywords, i.CatalogMenuItemContentItemId }).ToList().ToCsv(";"); } string attachment = "attachment; filename=myfile.csv"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "text/csv"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1"); HttpContext.Current.Response.Write(result); HttpContext.Current.Response.End(); public static string ToCsv<T>(this IEnumerable<T> items, string seperator = ",") where T : class { var csvBuilder = new StringBuilder(); var properties = typeof(T).GetProperties(); foreach (T item in items) { string line = string.Join(seperator, properties.Select(p => p.GetValue(item, null).ToCsvValue()).ToArray()); csvBuilder.AppendLine(line); } return csvBuilder.ToString(); } private static string ToCsvValue<T>(this T item) { return string.Format("{0}", HttpUtility.HtmlDecode(item.ToString())); } 

任何想法 ?