public string GetAsCSV() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); var m = new StringWriter(sb); TextWriter tw = m; var config = new CsvConfiguration(CultureInfo.CurrentCulture); config.Delimiter = ColumnDelimiter; var p = new CsvHelper.CsvWriter(tw, config); //CSVLine Line; foreach (var line in Lines) { foreach (var c in line.Columns) { p.WriteField(c); } p.NextRecord(); } p.Flush(); return(sb.ToString()); }
public static void WriteCollectionToCsv(IEnumerable records, Type classtype, string filename) { using (var sw = new StreamWriter(filename)) { var csvHelper = new CsvHelper.CsvWriter(sw); csvHelper.WriteHeader(classtype); csvHelper.NextRecord(); csvHelper.WriteRecords(records); sw.Flush(); csvHelper.Flush(); } }
public static void WriteObjectToCsv(Metrics metrics, Type classtype, string filename) { using (var sw = new StreamWriter(filename)) { var csvHelper = new CsvHelper.CsvWriter(sw); csvHelper.WriteHeader(classtype); csvHelper.NextRecord(); csvHelper.WriteField(metrics.Distance); csvHelper.WriteField(metrics.Number); sw.Flush(); csvHelper.Flush(); } }
public string GetCSVLine(string Delimiter = ",") { var sb = new StringBuilder(); var m = new StringWriter(sb); TextWriter tw = m; // new StreamWriter(m); var config = new CsvConfiguration(CultureInfo.CurrentCulture); //.Configuration(); config.Delimiter = Delimiter; var p = new CsvHelper.CsvWriter(tw, config); foreach (var c in Columns) { p.WriteField(c); } p.NextRecord(); p.Flush(); return(sb.ToString()); //var dbq = "\""; //System.Text.StringBuilder sb = new System.Text.StringBuilder(); ////string s; //bool First = true; //if (m_Columns == null) //{ // m_Columns = new List<string>(); //} //foreach (var s in Columns) //{ // if (!First) // sb.Append(Delimiter); // else // First = false; // sb.Append(dbq + s.Replace(dbq, dbq + dbq) + dbq); //} //return sb.ToString(); }