Search code examples
csvreporting-servicesexport-to-csvcsvhelper

Creating a csv file with single text item having length of 1125 characters


I am trying to create a csv file which has only field and its length is 1125 characters. When the extract is saved in CSV after 1024 characters records automatically goes to second line.

Any reason why and how to fix this limitation?


Solution

  • I tried a field with length of 1127 and opened the CSV file in Notepad++. I clicked on show characters and it shows the line break at the end of my field.

    public class Program
    {
        public static void Main(string[] args)
        {
            var bar = "bar";
            var data = bar;
    
            while (data.Length < 1125)
            {
                data = data + " " + bar;
            }
    
            var records = new List<Foo>
            {
                new Foo { Data = data }
            };
    
            using(var writer = new StreamWriter("path\\to\\file.csv"))
            using(var csv = new CsvWriter(writer))
            {
                csv.WriteRecords(records);
            }
        }
    }
    
    public class Foo
    {
        public string Data { get; set; }
    }