General List for CSV String

I have a list that I want to write to a CSV string.

The examples I found seem to be for separate lists of elements, I have several elements.

The code I 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 lot of time.

The class my list is based on the form:

 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 extended version of String.Join?

thanks

+6
source share
2 answers

Your method can be simplified using StringBuilder and string.Join .

Concatenating strings directly is slow and consumes a lot of memory, which is great for small operations.

See: Does StringBuilder use more memory than string concatenation?

 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(); } 

A more complete implementation for CSV:

 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; } 

Usage: escaping a complex string in CSV format

+17
source

we use linqtocsv with some success

https://linqtocsv.codeplex.com

and here is the explanation

http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library

0
source

Source: https://habr.com/ru/post/949641/


All Articles