本文介绍了泛型列表到CSV字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有我想要写一个CSV字符串列表。I have a list that I want to write to a CSV string.我已经找到了例子都似乎是为单个项目清单,该矿拥有多个项目The examples I have found all seem to be for single item lists, mine has multiple items.我目前已经是该代码;The code I currently have is;private static string CreateCSVTextFile<T>(List<T> data, string seperator = ",") where T : ExcelReport, new(){ var objectType = typeof(T); var properties = objectType.GetProperties(); var currentRow = 0; var returnString = ""; foreach (var row in data) { var currentColumn = 0; var lineString = ""; foreach (var info in properties) { lineString = lineString + info.GetValue(row, null) + seperator; currentColumn++; } if (seperator != "") { lineString = lineString.Substring(0, lineString.Count() - 2); } returnString = returnString + Environment.NewLine + lineString; currentRow++; } return returnString;} 但是,当列表很大这种方法需要很长的时间来运行。But when the list is large this method takes a very long time to run.我的列表是基于貌似类;The class my list is based on looks like;internal class ClientMasterFile{public String COL1{ get; set; }public String COL2{ get; set; }public String COL3{ get; set; }public String COL4{ get; set; }public String COL5{ get; set; }public String COL6{ get; set; }public String COL7{ get; set; }public String COL8{ get; set; }public String COL9{ get; set; }public String COL10{ get; set; }public String COL11{ get; set; }public String COL12{ get; set; }}有一个更快的方法来做到这一点使用字符串的高级版本。加入?Is there a faster way to do this using an advanced version of String.Join?感谢推荐答案您的方法可以使用简化 的StringBuilder 和 的string.join 。Your method can be simplified using StringBuilder and string.Join.连接字符串直接是缓慢的,使用了大量的内存,这是罚款,小规模行动。Concatenating strings directly is slow and uses a lot of memory which is fine for small operations.请参阅:是否StringBuilder的使用更多的内存比字符串连接?private static string CreateCSVTextFile<T>(List<T> data, string seperator = ","){ var properties = typeof(T).GetProperties(); var result = new StringBuilder(); foreach (var row in data) { var values = properties.Select(p => p.GetValue(row, null)); var line = string.Join(seperator, values); result.AppendLine(line); } return result.ToString();}一个更完整的实现了CSV的:A more complete implementation for CSVs:private static string CreateCSVTextFile<T>(List<T> data){ var properties = typeof(T).GetProperties(); var result = new StringBuilder(); foreach (var row in data) { var values = properties.Select(p => p.GetValue(row, null)) .Select(v => StringToCSVCell(Convert.ToString(v))); var line = string.Join(",", values); result.AppendLine(line); } return result.ToString();}private static string StringToCSVCell(string str){ bool mustQuote = (str.Contains(",") || str.Contains("\"") || str.Contains("\r") || str.Contains("\n")); if (mustQuote) { StringBuilder sb = new StringBuilder(); sb.Append("\""); foreach (char nextChar in str) { sb.Append(nextChar); if (nextChar == '"') sb.Append("\""); } sb.Append("\""); return sb.ToString(); } return str;}使用:的逃避棘手的字符串,CSV格式 这篇关于泛型列表到CSV字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 01:20