<pre lang="xml">/// </summary> public class CsvRow : List<string> { public string LineText { get; set; } } /// <summary> /// Class to write data to a CSV file /// </summary> public class CsvFileWriter : StreamWriter { public CsvFileWriter(Stream stream) : base(stream) { } public CsvFileWriter(string filename) : base(filename) { } /// <summary> /// Writes a single row to a CSV file. /// </summary> /// <param name="row">The row to be written</param> public void WriteRow(CsvRow row) { StringBuilder builder = new StringBuilder(); bool firstColumn = true; foreach (string value in row) { // Add separator if this isn't the first value if (!firstColumn) builder.Append(';'); // Implement special handling for values that contain comma or quote // Enclose in quotes and double up any double quotes if (value.IndexOfAny(new char[] {'"', ','}) != -1) builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\"")); else builder.Append(value); firstColumn = false; } row.LineText = builder.ToString(); WriteLine(row.LineText,Encoding.UTF8); } }
using (CsvFileWriter writer = new CsvFileWriter("WriteTest.csv")) { for (int j = 0; j < toExcelData.Count; j++) { CsvRow row = new CsvRow(); string[] bufzz = toExcelData[j].Split(';'); for (int i = 0; i<bufzz.Length; i++) { //for (int h=0; h<) row.Add(bufzz[i]); } writer.WriteRow(row); } }
StreamWriter
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)